tags:

views:

141

answers:

6

I've read about it on a message board - Random class isn't really random. It is created with predictable fashion using a mathematical formula.

Is it really true? If so, Random isn't really random??

+5  A: 

Because deterministic computers are really bad at generating "true" random numbers by themselves.

Also, a predictable/repeatable random sequence is often surprisingly useful, since it helps in testing.

unwind
+5  A: 

It's really hard to create something that is absolutely random. See the Wikipedia articles on randomness and pseudo-randomness

Xr
+2  A: 

The sequence is predictable for each starting seed. For different seeds, different sequences of numbers are returned. If the seed used is itself random (such as the DatetTime.Now.Ticks), then the numbers returned a adequately 'random'.

Alternatively, you can use a cryptographic random number generator such as the RNGCryptoServiceProvider class.

logicnp
+1  A: 

It is true, but you can always seed the random number generator with some time dependent value, or if you're really prepared to push the boat out, look at www.random.org...

In the case of the Random class though, I think it should be random enough for most requirements... I can't see a method to actually seed it, so I'm guessing it must automatically seed as built in behaviour...

Martin Milan
+2  A: 

It isn't random it's a random-like number generating algorithm and it's based on a number to generate. If you set that random number to something like the system time the numbers are more close to random, but if you use these numbers to lets say, an encryption algorithm, is the attacker knows WHEN you generate the random numbers and the algorithm you use, then it is more possible that your encryption will break.

The only way to generate true random numbers is to measure something natural, for example voltage levels or have a microphone picking up sounds somewhere or something like that.

Nikos
+3  A: 

As others have already said, Random creates pseudo-random numbers, depending on some seed value. It may be helpful to know that the .NET class Random has two constructors:

 Random(int Seed)

creates a random number generator with a given seed value, helpful if you want reproducible behaviour of your program. On the other hand,

 Random()

creates a random number generator with date-time depending seed value, which means, almost every time you start your program again, it will produce a different sequence of (pseudo-)random numbers.

Doc Brown