# Get random location in cuboid

Discussion in 'Plugin Development' started by Vincent1468, Sep 3, 2013.

Not open for further replies.
1. Offline

### Vincent1468

Hello,

If I have a cuboid, so two locations at each side, how would I get a random location in that cuboid?

Thanks.

#1
2. Offline

### metalhedd

It helps to think of the cuboid differently... it's not 2 locations per side. its 2 locations total. a minimum point, and a maximum point. when you look at it this way, your problem is to simply find 3 random numbers within 3 clearly defined ranges.

int deltaX = maxX - minX;
int deltaY = maxY - minY;
int deltaZ = maxZ - minZ;
int rx = random.nextInt(deltaX) + minX;
int ry = random.nextInt(deltaY) + minY;
int rz = random.nextInt(deltaZ) + minZ;

#2
Vincent1468 likes this.
3. Offline

### FurmigaHumana

I'm not sure if this will work, but give it a try:

Code:java
` Location start... // the first pointLocation end... // the second point public Location getRandomLocation() {    /* the the highest X, Y, Z value */    int maxX = Math.max(start.getBlockX(), end.getBlockX());     int maxY = Math.max(start.getBlockY(), end.getBlockY());    int maxZ = Math.max(start.getBlockZ(), end.getBlockZ()); /* the the lowest X, Y, Z value */    int minX = Math.min(start.getBlockX(), end.getBlockX());     int minY = Math.min(start.getBlockY(), end.getBlockY());    int minZ = Math.min(start.getBlockZ(), end.getBlockZ());     // create a new random location inside the limits    return new Location(start.getWorld(), nextInt(minX, maxX), nextInt(minY, maxY), nextint(minY, maxZ));} public int nextInt(int min, int max) {    Random rnd = new Random(); // the random thingy    return rnd.nextInt(max - min) + max; // get a new random number inside the limits} `

#3
Vincent1468 likes this.
4. Offline

### Vincent1468

FurmigaHumana
Thanks you, I'm trying this but I'm getting this error:
Code:
```java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at nl.imesiacraft.plugins.SpawnManager.nextInt(SpawnManager.java:83)
at nl.imesiacraft.plugins.SpawnManager.getRandomLocation(SpawnManager.java:77)
at nl.imesiacraft.plugins.SpawnManager.spawn(SpawnManager.java:31)
at nl.imesiacraft.plugins.Arena.start(Arena.java:152)
at nl.imesiacraft.plugins.Arena\$1.run(Arena.java:142)
at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:522)
at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
```
Line 83:
Code:java
` return rnd.nextInt(max - min) + max; // get a new random number inside the limits `

Line 77:
Code:java
` int z = nextInt(minZ, maxZ); `

And line 31:
Code:java
` Entity ent = world.spawnEntity(getRandomLocation(a.getL1(), a.getL2()), EntityType.ZOMBIE); `

#4
5. Offline

### sharp237

try:

return rnd.nextInt(Math.abs(max - min))+ max;

#5
Vincent1468 likes this.
6. Offline

### FurmigaHumana

maxZ should always be bigger, anyway... Change this:

To this:

Code:java
` return rnd.nextInt(Math.abs(max - min)) + max; // get a new random number inside the limits `

#6
7. Offline

### sharp237

Also, shouldn't it be:

Code:
`return rnd.nextInt(Math.abs(max - min) + 1) + min;`

#7
8. Offline

### Vincent1468

sharp237 FurmigaHumana
Tried both, same error.
Code:
```2013-09-03 21:44:17 [SEVERE] java.lang.IllegalArgumentException: n must be positive
2013-09-03 21:44:17 [SEVERE]at java.util.Random.nextInt(Unknown Source)
```

#8
9. Offline

### sharp237

try this:

Code:
```Bukkit.getLogger().info("n=" + (Math.abs(max - min) + 1));
return rnd.nextInt(Math.abs(max - min) + 1) + min;```
See what the console says.

#9
Vincent1468 likes this.
10. Offline

### Vincent1468

sharp237
Console says this:
Code:
```2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
2013-09-03 22:22:51 [INFO] n=3
2013-09-03 22:22:51 [INFO] n=1
```
And no error!
But for some reason all the zombies spawned on the same location.

#10