c# random number

Status
Not open for further replies.

kblueboi

Baseband Member
Messages
22
Random numb = new Random();
Random number = new Random();

Console.WriteLine(numb.Next(1,12));
Console.WriteLine(number.Next(1,12));

shouldn't that generate two different random numbers between 1 and 12? how come it doesn't?
 
Create only one random number generator to generate both numbers. As you see, the random number generators aren't really random. They are pseudorandom.
 
hmm ya ... although i'm still unsure as to why since i'm creating two random objects.

anyway, i found a way around it by instantiating Random number in a static method.
 
If you want to think about it, most random number generators incorporate the current system time as a varible to "randomize" the numbers.

When running your program, most of the time it will run in less than 1 second, so the random number generator will return the same "random" number. A simple way to get around this is by incorporating a pause between generating random numbers.
 
Status
Not open for further replies.
Back
Top Bottom