Possible Duplicate:
Random number generator not working the way I had planned (C#)
I have LinkedList of LinkedList, and when I try to dispaly after filling, the raws are the same, but the most curious is that in debug mode it dispalays different rows.
BTW I have VS11 beta.
private LinkedList<LinkedList<int>> grid = new LinkedList<LinkedList<int>>();
public void CreateMatrix(int rows, int coloumns, int maxSize)
{
for(int i = 0; i < rows;i++)
{
grid.AddFirst(generateList(coloumns, maxSize));
}
}
private LinkedList<int> generateList(int size, int maxSize)
{
var ranodGenerator = new Random();
var list = new LinkedList<int>();
for (int j = 0; j < size; j++)
{
list.AddFirst(ranodGenerator.Next(maxSize));
}
return list;
}
public void DisplayMatrix()
{
foreach (var list in grid)
{
foreach (var i in list)
{
Console.Write(i+ " ");
}
Console.WriteLine();
}
}
So after
MatrixManager matrixManager = new MatrixManager();
matrixManager.CreateMatrix(4,4,200);
matrixManager.DisplayMatrix();
it will display 4 identical rows
134 3 45 26
134 3 45 26
134 3 45 26
134 3 45 26
but should display different
This should be duplicate of “How to use Random”
This line restarts creation of random numbers. Create one random number generator and life will be much better.
Here is the FAQ: Random number generator only generating one random number