Get random location within radius

Discussion in 'Plugin Development' started by funkystudios, May 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    funkystudios

    A feature in the plugin I'm developing includes spawning a chest on the surface at a random location. The issue is that the location must be within a 250 block radius from the world spawn.

    Is there a formula of some kind that would accomplish this task? How would go about this?
     
  2. Code:java
    1.  
    2. Location startFrom;
    3. Location output = startFrom.clone();
    4. output.add((random.nextBoolean() ? 1 : -1) * random.nextInt(radius),
    5. (random.nextBoolean() ? 1 : -1) * random.nextInt(radius),
    6. (random.nextBoolean() ? 1 : -1) * random.nextInt(radius));
    7. return output;
    8.  

    triangular distrubution code:
    Code:java
    1.  
    2. Location startFrom;
    3. Location output = startFrom.clone();
    4. output.add(random.nextInt(radius) - random.nextInt(radius),
    5. random.nextInt(radius) - random.nextInt(radius),
    6. random.nextInt(radius) - random.nextInt(radius));
    7. return output;
    8.  
     
    Tempelchat likes this.
  3. Offline

    funkystudios

    Brilliant! Thank you very much!
     
  4. Whats the difference between the two? Also seems that the y changes as well..
     
  5. the first 1 have sqaure distribution, the second triangulair
     
  6. Is the below graphic correct?

    For future people who stumble upon this.. Here is a graphic to help you understand..

    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page