[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?