Search code examples
c++csecurityrandomentropy

Seeding pseudo random generator with random number from a better random source


Let's say we have a pseudo random number generator that accepts a 32 bit integer as seed (for example the rand() function from the C standard library), which derives random numbers from the original seed. If that seed were to come from, let's say, radioactive decay, is it correct to assume that the random number we get from calling rand() is as "good" random number as taking generating one from radioactive decay?


Solution

  • No, definitely not. The C/C++ standard library's builtin rand() function is usually implemented as a linear congruential generator (LCG). It is among the earliest known family of pseudorandom number generators (PRNGs) and they generally have notoriously bad statistical properties. Furthermore since PRNGs actually produce a mathematical sequence predetermined by an initial seed, they are predictable. Even cryptographically secure pseudorandom number generators (like the Blum Blum Shub) are predictable, even if it's computationally difficult and very time consuming to predict the sequence.

    In contrast, random number generators based on radioactive decay are true random number generators. The generated sequence of numbers is perfectly uniformly distributed and unpredictable, without any measurable correlation between the samples.

    Back to pseudorandom numbers, the statistical quality of the source of the initial seed doesn't improve the statistical quality of the generated pseudorandom sequence - it only depends on the generator itself. If you use a true random number to seed a PRNG, then the first number of the sequence will be unpredictable, but then the quality of the sequence will be the same as it would be without the true random seed.

    If you want high quality of randomness, you have to use a high quality random number generator. There are pseudorandom number generators with excellent statistical properties (definitely not the famous Mersenne Twister), passing all current statistical tests of randomness - while the generated pseudorandom sequence is still predictable, statistically it's hard to distinguish from a truly random sequence.

    A good reliable resource on modern random number generators is Sebastiano Vigna's website.