Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate truly (not pseudo) random numbers with C#?

Tags:

c#

algorithm

math

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

like image 866
Max Avatar asked Aug 05 '09 15:08

Max


People also ask

Can you generate random numbers in C?

In the C programming language, the rand() function is a library function that generates the random number in the range [0, RAND_MAX].

Is rand () truly random?

What that means is that, technically speaking, the numbers that Excel's RAND functions generate aren't truly random. Some companies want to get as random numbers as possible and get creative as to where their random data comes from (like CloudFlare's wall of Lava Lamps).


1 Answers

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

The Easy Way (for simplicity & practicality)

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.

The Hard Way (for high quality theoretical randomness)

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.

like image 196
Noldorin Avatar answered Sep 18 '22 09:09

Noldorin