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

VulpesRank #55 · 29.2K reputationPosted Apr 19, 2014, 7:56 AM EDT
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);
Amit Ziv42 reputationPosted Apr 19, 2014, 9:11 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 19, 2014, 10:53 AM EDT
Munesh SharmaRank #174 · 13.9K reputationPosted Apr 19, 2014, 11:29 AM EDT
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);