# Math / Logic Help with 2d tile map generation

So my brain is melting trying to figure this out(also I’m new to all of this). It’s probably not that hard but I’m stumped.

I am making a world generator for my 2d sprite top down RPG.

just for example say i have a 10x10 tile map(grid)
and say I have 4 different types of tiles and each tile has its own percentage change of becoming what tile is actually generated in that current x,y pos.

Just trying to figure out the correct way to do this. So I figured I would make variables for type of tile(I made them floats in case i ever want there to be a very low change of a tile being generated, something below 1 percent):

//density floats represent percentages. ex: 0.0f = 0 percent chance of that tile type to spawn. 100f = 100 percent
public float grassDensity = 75f;
public float dirtDensity = 40f;
public float waterDensity = 35f;
public float snowDensity = 20f;

So just to clarify I’m trying to make it so i can set the chance based on percent that each tile type has to actually be spawned at that pos on the grid.

and then of course i do this:

for (uint y=1; y<worldWidth + 1; y++) { // y is vertical
for (uint x=1; x<worldHeight + 1; x++) { // x is horizontal

P.S.
I discovered that I had to use uint in the for loop instead of just int variables for the x and y. can anyone tell me why the normal int wouldn’t work?

When dealing with percentage chances, they should normally add up to 100%

What you can then do, is arrange them with cumulative values like this

Then you simply generate a random number from 0 to 100, and use whichever band it falls in, so

``````if(randomValue < 40) grass
else if randomValue < 50 snow
else if randomValue < 70 sand....//etc
``````

Of course, usually this is handled in the 0=>1 range.

int should work fine, no need for uint.