[SOLVED]Random Generated 2D world

I got inspired of the small game “MiniCraft”.

How is world randomly generated by code?

Example: I have a world which is the size 2000,2000
How can i make by code that for example trees are randomly placed around on this world?

Im thinking using nested for loops to make it go place on random locations, But also if i manage to for example 10 trees gets randomly placed on the map how do i make not something else spawn where the tree spawns… Maybe an if statement that checks (array[posx][posy]==0) and then its ok to place for example a rock here?

Look into java.util.Random

There are two basic ways to randomly populate a world with trees.

-Use random numbers to find the positions to place trees.
-Random chance to place a tree for each position.

Each has opportunities for more randomness.

Other options include smooth noise functions (Perlin & Simplex etc.) with a threshold to decide tree/no tree.

Alright thank you,
I looked through the java.util.random but i dont see any method to get a random int between a certain number…

Lets say i want an int between 0 and 5000.

nextInt(int)

For not explicitly storing the location of all random placeables look here: http://www.java-gaming.org/topics/uniform-feature-points/28000/view.html

I mean how to make nextInt(int) only give back a fixed number for example:
I only want a random number between 0-100…

Thats what he sayd.
int value = nextInt(100); for values between 0-100

Oh wow, Sorry i didnt understand that.

So nextInt(100) as an example would give me a number between 0-100 or could it give me negative numbers too?

It’ll give you values from 0 - 100… no negative values :wink:

Great! Big thanks to everyone!!

nextInt(100) will return values between 0 and 99…so 100 possible results. (Hint: read the documentation…if you’re using an IDE it should automagically display it for you)

for values between 0 - 1 you could use .nextBoolean() :wink: