Monday, April 30, 2012

Why is System.Random giving '1' a lot of times in a row, then not for a while, then again?

Not sure how else to explain this, so the title pretty much describes the problem.



Random is not being re-initialised every part of the loop. It's a static member of a class which I always call on from other classes.



I am not using a custom seed.



The initialisation code is:



    public static Random random = new Random();

for (int x = 0; x < 75; x++)
{
if (main.random.Next(11) == 1)
{
tiles[heightMap[x] - 1][x] = 4;
tiles[heightMap[x] - 2][x] = 4;
tiles[heightMap[x] - 3][x] = 4;
tiles[heightMap[x] - 4][x] = 4;
tiles[heightMap[x] - 5][x] = 4;
tiles[heightMap[x] - 5][x - 1] = 5;
tiles[heightMap[x] - 6][x - 1] = 5;
tiles[heightMap[x] - 6][x] = 5;
tiles[heightMap[x] - 5][x + 1] = 5;
tiles[heightMap[x] - 6][x + 1] = 5;
}
}


This (Ignore my ugly hax for making a tree, it's rudimentary and temporary) generates a tree.



However my terrain often looks like:



AIR AIR AIR AIR TREE TREE TREE AIR AIR TREE TREE AIR AIR AIR AIR TREE TREE TREE



As in it seems to get clusters of 1 at a time.



Can anyone give insight into why this is happening? Is there a better alternative than using the System.Security.Cryptography.Random class (Since that's slower and this is a game not a bank program I want it to have very fast loading speeds)



I'd expect an average of 9 gap per tree, but it's more like 7 and then 3 trees closely clustered together.



enter image description here





No comments:

Post a Comment