Possible Duplicate:
Random number generator not working the way I had planned (C#)
I created a method that returns me a random number:
public static int SelectRandomMachine(int max)
{
int seed = (int)DateTime.Now.Ticks;
Random rndNumber = new Random(seed);
int randMachine = rndNumber.Next(0, max);
return randMachine;
}
if I call the method two times, currently it’s return me the same random number:
randM1 = SelectRandomMachine(maxNumber);
randM2 = SelectRandomMachine(maxNumber);
any suggestion would be highly appreciated.
Hint look at this line:
If you execute that line twice in quick succession, what do you think the values will be?
For example:
See my article on randomness for solutions and more information.
EDIT: Here’s a quick and dirty example of the lack of thread safety causing problems:
Output on my box: