Are Random Number Generators Truly Random ? Debunking the Myth
Published: 2024-01-11 17:35:41


![Image of a joke related to randomness](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jj6dikm6gps4q6ee10kp.jpg)

## 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](https://docs.python.org/3/library/random.html) module.

## Random Number Generators from Scratch

Let's start with a common but effective random number generator [Linear Congruential Generator](https://en.wikipedia.org/wiki/Linear_congruential_generator), the algorithm of this generator is something like this:

![Formula of Linear Congruential Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rqdgb4y9w60wolqgsug.png)

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](https://en.wikipedia.org/wiki/Linear_congruential_generator).

![Code of Linear Congruential Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtzo3mekz5mr0wlzck17.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/covsuhzpdcqg26ir1leo.png)

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](https://en.wikipedia.org/wiki/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](https://en.wikipedia.org/wiki/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r5wycrn0nja70164qkb.png)

## 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](https://www.random.org/).