Reputation: 61
I have some code that runs. Inside of a loop I execute:
System.Random rnd = new System.Random();
Can I expect the number to be random if there is almost no time delay between running the call?
Upvotes: 1
Views: 2678
Reputation: 31202
Do you need random or pseudo random numbers ? What about System.Security.Cryptography.RandomNumberGenerator ?
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx
Upvotes: 1
Reputation: 437376
From the documentation:
Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.
The delay between generating two random numbers does not come into account. Of course this refers to calling Random.Next
on the same instance instead of creating a new Random
instance each time as in your code (which is wrong):
This problem can be avoided by creating a single Random object rather than multiple ones.
To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating a new Random objects to generate one random number.
If you need to generate cryptographic-strength randomness:
To generate a cryptographically secure random number suitable for creating a random password, for example, use a class derived from System.Security.Cryptography.RandomNumberGenerator such as System.Security.Cryptography.RNGCryptoServiceProvider.
Upvotes: 5
Reputation: 10754
No because a call to new System.Random(); without parameters will use the system clock as a seed and so possible to get the same random numbers in your loop.
You should create new System.Random() object outside of your loop and get the next random number inside the loop.
rnd.Next();
Upvotes: 2
Reputation: 1500515
No, you'll almost certainly get the same numbers twice. You want to use a single instance of Random
for the whole sequence... so long as you're doing everything in one thread.
It's unfortunate that Random
has this issue and isn't thread-safe - so the simple approach of having a single static variable initialized once doesn't work :(
There are a few approaches to solving this, including creating an instance per thread.
I have an article which goes into a lot more detail, which you may find helpful.
Upvotes: 3
Reputation: 6637
Yes it will give random values, I dont think it has anything to do whether delays exists between the call or not. Declare and initialze the random variable outside the loop.
Upvotes: 0
Reputation: 72039
Why not move that line outside of the loop and only call the Next()
method inside of the loop?
Upvotes: 2
Reputation: 66389
It won't be random as you'll use the same seed.
Have that line only once, outside the loop, and use the same rnd
instance in every iteration, calling its Next
method.
Some basic explanation: computer can't generate "real" random number. The trick is taking some unique seed which is usually large integer number than use special algorithm to make that seed into number in the desired range. Same seed will produce same sequence of "random" numbers every time it will be called.
As far as I know, the default seed used by C# is the amount of milliseconds that passed since 1/1/1970.
Upvotes: 1
Reputation: 32428
You shouldn't create a new Random instance each time you want a random number. You should create it once, then use the Next... methods.
Providing you do that it'll be random whatever the time delay.
Upvotes: 5