# Random location within coords?

Discussion in 'Plugin Development' started by lcpvp, Feb 21, 2013.

Not open for further replies.
1. Offline

### lcpvp

How would I set a random location within a set of x, y and z coords? I am trying to build something that goes to random coords inside of a border of x, y and z coords.

For example:

If border is 100, and I want diamond block to be placed, then it just is randomly place somewhere in that border.

#1
2. Offline

### Deckerz

Just generate the co ords and do a few IF statements check if it in the range and if it isn't repeat the method

#2
3. Offline

### lcpvp

How to generate coords?

#3
4. Offline

### Deckerz

Random rnd = new Random();
int x = rnd.nextInt();
int y = rnd.nextInt();
int z = rnd.nextInt();
Location loc = new Location(x, y, z);

#4
5. Offline

### vildaberper

Thats a very bad solution.

You should rather do something like:
Code:
```int x = rnd.nextInt(maxX - minX + 1) + minX;
int y = rnd.nextInt(maxY - minY + 1) + minY;
int z = rnd.nextInt(maxZ - minZ + 1) + minZ;```
But of course it's better to make a method for random integers in a Util class or something like that.
Code:
```public static int getRandomInt(int min, int max){
return new Random().nextInt(max - min + 1) + min;
}```

#5
6. Offline

### Deckerz

does it matter really?

#6
7. Offline

Yes!

#7
8. Offline

### Deckerz

not really
it will affect the CPU usage by the smallest amount

#8
9. Offline

### vildaberper

"All 2^32 possible int values are produced with (approximately) equal probability."
Simple math, there is a 100((max-min)/2^32)% chance of getting it right for every loop...

That's just stupid.

#9
10. Offline

### Deckerz

oh in the bracket i just realised i did put maxInt

#10
11. Offline

### lcpvp

What do you guys mean by maxX and minX?

#11
12. Offline

### vildaberper

Reply next time so I get a notification, lucky I lurk thease forums.
Well isn't that pretty self-explanatory? You define a range for the integer by setting the min and max values.

#12
13. Offline

### lcpvp

Thanks, for minx would I put -500? if i wanted max and min to be 500 , - 500

Code:java
`             Player player = player.getPlayer();            Block block = player.getTargetBlock(null, 8);            Location location = block.getLocation();            World world = player.getWorld();            Random random = new Random();            int x = random.nextInt(500 - -500 + 1) + -500;            int y = random.nextInt(128 - -128 + 1) + -128;            int z = random.nextInt(500 - -500 + 1) + -500;  `

So I want to spawn dirt at ground level at the location, and then where that location is, build of more dirt from it. So if the random coords are 300, 300, it places a grass block, and then I can build off that so its like a circle of dirt blocks.

Does anyone know how to build structures like this using that code?

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 31, 2016
#13
14. Offline

### vildaberper

If I understood your question right you want to create a structure at a random ground-level position.
So you don't need to generate the y-coord, just do:
Code:
`int y = world.getHighestBlockAt(x, z);`
After that you can simply make the structure relative to that block.

#14
15. Offline

### lcpvp

Code:java
`             Player player = player.getPlayer();            Block block = player.getTargetBlock(null, 8);            Location location = block.getLocation();            World world = player.getWorld();            Random random = new Random();            int x = random.nextInt(500 - -500 + 1) + -500;            int z = random.nextInt(500 - -500 + 1) + -500;            int y = world.getHighestBlockAt(x, z);             } `

So now, what works? Im on my phone, but would I do world.getChunkAt(x, z);
and then replace or what?

#15
16. Offline

### evilmidget38

lcpvp You would change the blocks into the structure. Be a problem solver. All you do is come to the forums and ask for help in every category until people end up giving you the code. Do what programming is actually about and try solving the problem yourself.

Here's a good thought process that should help you get started, since you seem not to have one:

How do I change a single block?

How do I change two blocks?

How do I change three blocks?

How do I change four blocks?

How do I change all the blocks in the shape of the structure?

Additionally, think about what the code does. Why would you need the chunk to change some blocks? Why do you want a coordinate where the x and y values are between 501-1000?

#16
Adamki11s and LaxWasHere like this.