ive created 2 seperate randoms:
private Random r1 = new Random();
private Random r2 = new Random();
then i did the random.next method:
maxGreenDelay = r1.Next(10, 40);
maxRedDelay = r2.Next(10, 40);
and the same numbers appeared.
how can i avoid that? (get 2 different randoms withing this min - max values)
Loading
VulpesPosted Apr 19, 2014, 11:56 AM
One way to do this is to make the seeds time dependent:
private Random r1 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
private Random r2 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);
Munesh SharmaPosted Apr 19, 2014, 3:29 PM
var rnd = new Random();
for(int i = 0; i < 100; ++i)
Console.WriteLine(rnd.Next(1, 100));
The sequence of random numbers generated by a single Random instance is supposed to be uniformly distributed. By creating a new Random instance for every random number in quick successions, you are likely to seed them with identical values and have them generate identical random numbers. Of course, in this case, the generated sequence will be far from uniform distribution.
For the sake of completeness, if you really need to reseed a Random, you'll create a new instance of Random with the new seed:
rnd = new Random(newSeed);
VulpesPosted Apr 19, 2014, 2:53 PM
Amit ZivPosted Apr 19, 2014, 1:11 PM