# Percent that "x" tile type spawns

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?

You can use the [alias method][2] to sample from an arbitrary discrete probability distribution in constant time, with a little set-up work.

There’s an [excellent step-by-step tutorial here][3], which walks through different sampling methods. The last half describes the alias method and a particularly nice implementation of it called Vose’s algorithm.

![Initial probability distribution][4]

The basic idea is that you build a table with as many entries as you have choices of output. Then each entry is assigned a probability for one output, and the remainder for another. (Details of how to create this table at the link)

![Table with at most two choices per entry][5]

Now sampling from the distribution is a simple two-step task: select an entry from the table randomly with an even distribution (eg. using Random.Range), and then select one of the two outputs for that entry (eg. by comparing Random.value < entry.probability), then return the selected output.

Update:
[26126-discretedistribution.zip*_|26126]

Usage example:

(here I assume that your tile set is stored as an array of tile data structures, and that each has a weight property indicating how often that tile should spawn)

``````// Setup: (do this once)
float[] tileWeights = new float[tileSet.Length];
for(int i = 0; i < tileSet.Length; i++)
{
tileWeights _= tileSet*.weight;*_
``````

}

DiscreteDistribution tileDistribution = new DiscreteDistribution(tileWeights);

// Then, to spawn a random tile:
int tileToSpawnIndex = tileDistribution.Sample();
Tile tileToSpawn = tileSet[tileToSpawnIndex];
_*
[2]: Alias method - Wikipedia
_[3]: http://www.keithschwarz.com/darts-dice-coins/*_
_
[4]: http://www.keithschwarz.com/darts-dice-coins/images/aliasMethodInitialProbabilities.png*_
_*[5]: http://www.keithschwarz.com/darts-dice-coins/images/aliasMethodSetup4.png*_

thanks again. ill play with it as soon as i get to that part. right now im working on the script / scene that generates the game world so wont be long. but im having issues with that as well. if your feeling extra helpful you can check that out lol

Other issue.