DEV Community

Aditya Tewary
Aditya Tewary

Posted on

Are Random Number Generators Truly Random ? Debunking the Myth

Image of a joke related to randomness

Introduction to Randomness

Randomness refers to the unpredictability and lack of pattern in events or sequences. Contrasting with determinism, where outcomes are entirely predictable. In our blog, we will discuss random numbers particularly and learn whether we can actually generate random numbers using computer algorithms or not. We will also take a look to Python's random module.

Random Number Generators from Scratch

Let's start with a common but effective random number generator Linear Congruential Generator, the algorithm of this generator is something like this:

Formula of Linear Congruential Generator

Don't worry, we are not going to do any math by ourselves but we will use Python to write the algorithm and generate random numbers for us. The initial values which will be used for our Linear Congruential Generator are based on the specifications mentioned in its Wikipedia page.

Code of Linear Congruential Generator

So far we are good, we have generated some random numbers using Python but... wait are they truly random numbers ?

If we revisit our first sentence in this blog:

Randomness refers to the unpredictability and lack of pattern in events or sequences.

According to this definition, a random number should be unpredictable and lack any patterns but in the case of Linear Congruential Generator if we know the initial values of the algorithm, we can easily predict the next value and not only that we can also generate a pattern of these numbers. Let's plot the output of our above written code:

Plot of Linear Congruential Generator

This clearly shows that we have a cyclic pattern from the values generated by the random number generator. Hence, disproving the definition of randomness.

So let's come to the conclusion, if Linear Congruential Generator is not a true random number generator then what is it ?

Linear Congruential Generator is a pseudorandom number generator.
A pseudorandom number generator (PRNG) is a computer algorithm that produces sequence of numbers that appears to be random, but they are generated deterministically. PRNGs use mathematical formulas to produce sequences that mimic randomness.

Introduction to Python's random module

As we have understood as of now that we cannot generate a true random number using mathematical formulas and Python's random module is no exception. The Python random module uses Mersenne Twister which is a popular and robust psuedorandom number generator.

The Mersenne Twister starts with an initial value known as a seed (Xn). From this seed, it uses a complex mathematical formula to generate a long sequence of numbers. What makes the Mersenne Twister stand out is its long period, meaning it can produce a vast number of different values before repeating. It's like a very long playlist of songs that doesn't repeat for a very long time. It can be used for most practical purposes but it's important to understand that with initial seed we can predict the whole sequence. So, it's not truly random but rather psuedorandom.

Let's also check by plotting some random numbers generated using Python's random module:

Plot of Mersenne Twister

Conclusion

In unraveling the world of randomness and exploring random generators, we've journeyed from the basic principles of unpredictability to algorithms like Linear Congruential Generator and Mersenne Twister. While these generators provide valuable pseudorandom sequences for various applications, we can get true randomness only using some external physical variable that is unpredictable, such as radioactive decay of isotopes or any other atmospheric noise.

If you are really curious to check a true random generator then you can check here.

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Nothing is truly random. The concept of true randomness is an unreachable ideal. By definition, if you have a method to generate a random number, it isn't truly random.