The Mystery of Random Numbers
Tags: programming, beginners, python
Published: 2022-01-27 16:12:58

Today we will have a look at how your computer really generates a random number and most importantly is it truly ‘random’?

## What’s a Random number actually is?

So to understand what a random number is we first need to understand what ‘_Random_’ actually means. Let’s say you want to throw a dart to a board that has a particular value for each landing position of your dart. Here you cannot predict the outcome. Will it be a 10 or 540, no one really knows (unless you are the Dennis Ritchie of throwing darts!). Now that will be a random output as we really don't know the outcome. So in a nutshell a **random number is a number or output which does not have any dependency or is impossible to predict**.

## How computers really produce a random number?
This brings us to the next question, how my laptop or smartphone produces a random number? Ahem… **it** **does not**.
Really!?
Yes, _it does not_. This is the truth. No deterministic computing system can produce a truly random number, never. No matter which system, OS, or programming language you are using. No number generated with `randint()` or similar function can ever give you a truly random number. It will always be using something that is already in the system, like your mouse movement or your keystroke interval, etc. This will be the reference and then the system will apply some logic that is predefined and will finally give you a ‘random’ number. See the loophole? If you know the base number you can reproduce the same number in no time. Confused? Let’s check it out in Python,

```
>>> import random
>>> random.seed(20)
>>> random.randint(0, 1000)
927
>>> random.seed(25)
>>> random.randint(0, 1000)
386
>>> random.seed(20)
>>> random.randint(0, 1000)
927
```

Here using `seed()` we are **setting the base value** from where python should start applying its logic to generate the ‘random’ number. If you make the base same each time you will get the same output. As all the logics are predefined and our computer cannot think like a human yet we will get the same value each time. Now you might be thinking about AI/ML. Actually no. Not even the best AI in the world can do this. As I said earlier, a deterministic system will never be able to give us a truly random number. True randomness can never be generated fully by programming.

## Okay, but why the heck I am writing this?
You might be thinking so what? Why this guy is wasting his time by writing this? Well, You may not notice but random numbers are everywhere. Even when you are reading this article here you are actually taking the help of a random number. Random numbers are the base of **Cryptography**. One of the most popular Asymmetric cryptographic algorithm, the **RSA algorithm** uses a random number to create a Public and Private key, like many other algorithms. This random number generation is crucial for the algorithms to maintain their strength and hide your personal data behind a safe vault. As I demonstrated above if a hacker gets to know or guessed the random number then it will be really very easy for them to crack the rest of it.

## So can we really make our computers deliver a truly random number?
Well as of now it is not possible with your or my PCs, but we can look for other options. With the help of **Quantum computers**, we will be able to generate _a truly random number_. it's actually a basic feature of Quantum computers. So that's being said I will leave for the day. I might come back for a deep dive session on this and how the Quantum computers actually work, but hey who knows, after all, human behavior is a random thing right? Right?!