Sign in to follow this  
menyo

C sharp random favors higher numbers?

Recommended Posts

menyo    972
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.

[code]
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;
}

[/code]
etc..etc...

this is the land generator:
[code]
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++;
}

}
[/code]

Forgive me if the code seems a little cluttered i'm always making a mess when trying stuff out :D.

Share this post


Link to post
Share on other sites
Nypyren    12063
[url="http://msdn.microsoft.com/en-us/library/aa329893(v=VS.71).aspx"]http://msdn.microsoft.com/en-us/library/aa329893(v=VS.71).aspx[/url]

[quote]

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

[/quote]


Share this post


Link to post
Share on other sites
menyo    972
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.

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='Nypyren' timestamp='1313117218' post='4848072']
[url="http://msdn.microsoft.com/en-us/library/aa329893(v=VS.71).aspx"]http://msdn.microsof...3(v=VS.71).aspx[/url]

[quote]

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

[/quote]



[/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.

Share this post


Link to post
Share on other sites
NightCreature83    5002
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this