Max - 2 months ago 19

C# Question

I know that the Random class can generate pseudo-random numbers but is there a way to generate truly random numbers?

Answer

The answer here has two main sides to it. There are some quite important subtleties to which you should pay due attention...

The `RNGCryptoServiceProvider`

, which is part of the Crypto API in the BCL, should do the job for you. It's still technically a pseudo-random number generated, but the quality of "randomness" is much higher - suitable for cryptographic purposes, as the name might suggest.

There are other crypographic APIs with high quality pseudo random generaters available too. Algorithms such as the Mersenne twister are quite popular.

Comparing this to the `Random`

class in the BCL, it is significantly better. If you plot the numbers generated by `Random`

on a graph, for example, you should be able to recognise patterns, which is a strong sign of weakness. This is largely due to the fact that the algorithm simply uses a seeded lookup table of fixed size.

To generate *truly* random numbers, you need to make use of some natural phenomenon, such as nuclear decay, microscopic temperature fluctuations (CPU temperature is a comparatively conveient source), to name a few. This however is much more difficult and requires additional hardware, of course. I suspect the practical solution (`RNGCryptoServiceProvider`

or such) should do the job perfectly well for you.

Now, note that if you *really do require truly random numbers*, you could use a service such as *Random.org*, which generates numbers with very high randomness/entropy (based on *atmospheric noise*). Data is freely available for download. This may nonetheless be unnecessarily complicated for your situation, although it certainly gives you data suitable for scientific study and whatnot.

The choice is yours in the end, but at least you should now be able to make an informative decision, being aware of the various types and levels of RNGs.