I am attempting to randomly place the letter "x" into slots in a 2D array as an initializer for a Conway Game of Life.
string[,] initialArray = new string[rowMax, colMax];
for(int row = 0; row < rowMax; row++)
{
for(int col = 0; col < colMax; col++)
{
if(randomNumber() < 7)
{
initialArray[row, col] = " ";
}
else
{
initialArray[row, col] = "x";
}
tbGrid.Text += initialArray[row, col];
}
tbGrid.Text += "\r\n";
}
What I want to appear is a somewhat random placement such as :
x x x xx xx x
x x x
etc.
However, what I'm ending up with is more grouped together such as:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Just for example.
Any idea why that might be? At first I thought it was because I had the tbGrid.Text statement inside the IF statement. Taking that out helped, but it still didn't perform as desired.
The random number generator is set as follows:
public static int randomNumber()
{
Random randomNum = new Random();
int random = randomNum.Next(1, 10);
return random;
}
If that's helpful. Ideas? Suggestions?
Aucun commentaire:
Enregistrer un commentaire