C sharp random favors higher numbers?

Started by
3 comments, last by NightCreature83 12 years, 8 months ago
Hi,

I have been working on a 2D random tilemap map generator algorithm. At first i just started a rand.next(-2,2) for the height of the next tile. Almost each map sky rocketed out of my array. So i tried rand.next(-200,200)/100 and that worked out a bit more but still my map graduatly goes up.

Now i implemented a lot more, some of that random but i added the code below too, it does seem to flat it out some more but out of the 100 times i generated the map the right end of the map was only 1 time lower then the left end. Maybe i'm paying too much attention to it since it was so obvious whit the rand.next(-2,2) but it still makes me wonder.


int shaper = rand.Next(0, 1000);
//flat
if (shaper < 100)
{
int collumns = rand.Next(10, 20);
if (tileCollumn + collumns >= endTile) collumns = endTile - tileCollumn;
LandGenerator(startTile + (heightCount * mapWidth), collumns, -1, 1, minTotalHeight, maxTotalHeight); //<-- This one seems to never increase or decrease hight... weird... I am seeing a lot of flat area's without a single bump indent or a single step up or down.
startTile += collumns;
tileCollumn += collumns;
}
else if (shaper < 300)
{
int collumns = rand.Next(5, 15);
if (tileCollumn + collumns >= endTile) collumns = endTile - tileCollumn;
LandGenerator(startTile + (heightCount * mapWidth), collumns, 0, 2, minTotalHeight, maxTotalHeight);
startTile += collumns;
tileCollumn += collumns;
}


etc..etc...

this is the land generator:

private void LandGenerator(int startTile, int collumns, int minHeight, int maxHeight, int minTotalHeight, int maxTotalHeight)
{

int step;
Random rand = new Random();

for (int i = 0; i < collumns; i++)
{
//TODO: Lower the possibility of the max and min numbers.
step = rand.Next(minHeight * 100, maxHeight * 100) / 100;

if (heightCount + step > minTotalHeight && heightCount + step < maxTotalHeight)
{
startTile += (step * mapWidth);
heightCount += step;
}

int location = startTile;

for (int y = location; y < mapHeight * mapWidth; y += mapWidth)
{
tiles[y].textureName = "dirt";
}

//increment start tile to the next collumn
startTile++;
}

}


Forgive me if the code seems a little cluttered i'm always making a mess when trying stuff out :D.
Advertisement
http://msdn.microsoft.com/en-us/library/aa329893(v=VS.71).aspx



Return Value
A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not MaxValue. If minValue equals maxValue, minValue is returned.

[/quote]


Even more strange.... It isn't able to pick the max vallue and my map terrain goes up i must have something else in my code that make the terrain goes up somehow.

http://msdn.microsof...3(v=VS.71).aspx



Return Value
A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not MaxValue. If minValue equals maxValue, minValue is returned.





[/quote]

I'm not sure I get the significance of your bolded statement. I believe that is simply saying if you do .Next(0,5) the return values will include 0,1,2,3,4 but not 5.
I think there is something else going on, we as humans are bad at spotting true randomness. This is because our brains are wired to look for patterns in everything, so when we see a random something we still try to see a pattern.

Your best bet is to graph out how often the numbers in your interval are generated, run this for a big number by the way otherwise you can't trust your data, if that graph is truly favoring an edge or a particular range then there is something wrong. And I mean like big deviations outside of what you expect to see, so 3 or 4 times the average.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement