DEV Community

Prakhar Tandon
Prakhar Tandon

Posted on • Updated on

Is random() really random ?

Okay I admit, I am obsessed with all this "random" concept, my third article on this at dev community.
As we all know, computers are dumb machines, they just follow some algorithms, steps programmer tells them to do. So how can a computer randomly generate a number then? And even more importantly, on what basis we say something is actually random?
I spent this week researching over these questions that arose in my mind, and this article will be kind-of summary about that. So follow me for more of such content and connect with me on Twitter as well! (self endorsement is important).

So first let's dive into the basic stuff. We all must have used Math.random() in JavaScript or srand() and rand() in C++ and similar counter-parts in all other languages. So how do they really work ?

These functions use some pre-defined algorithms to generate a random number. And as you might have got the question, "if it's predefined procedure, then how can it be really random ?"
Well yes, they aren't TRULY Random, they are called PSUEDO Random Number Generators.

If you want to dive deep into the algorithms of the same, here is one : Linear congruential generator
The method represents one of the oldest and best-known pseudo random number generator algorithms.

Also if you want me to write over these algorithms in simple words, do let me know in the comment section or my Twitter:)

So lets get back to our point, then how can we generate Truly Random number?
And even before this, why we actually need one? What’s the purpose of all this True Random thing ?

Well, randomness has a lot of applications in real life. Say for instance you are making a Dice game as your project, you will need a random() to generate a number in the dice. In previous times and still in general purposes, things such as a coin flip or a dice are supposed to be random or say fair for all parties involved. But is it really ?

Let’s have a look at Physics behind a coin flip.

There are multiple factors on which the result of coin flip may depend -
1) Which face is currently at the top
2) The weight distribution of the coin
3) The angular velocity and torque provided while flipping
4) The linear velocity and acceleration
5) Air density, pressure, humidity, air speed.
6) The friction coefficient of the floor where the coin lands and so on !!

There dozens of such factors involved in a simple coin flip, which makes it difficult to predict or manipulate the result, but it’s difficult, not Impossible !

Here’s an news article about that : Euro coin accused of unfair flipping

What makes such things feel random to us is the complexities involved, which are difficult for a normal person to solve.

And another very major and important application of randomness is Cryptography!
I will cover this one in my next article ( Link coming soon )

OKAY so just answer the question! How can we get Truly Random Numbers !!

Simple answer is “EXTERNAL ENTROPY”.

So, what’s entropy? Entropy is the measurement of uncertainty or disorder in a system. Good entropy comes from the surrounding environment which is unpredictable and chaotic. You can think of entropy as the amount of surprise found in the result of a randomised process: the higher the entropy, the lesser is the certainty found in the result.

Atmospheric noise, cosmic radiations, quantum particles are currently the best physical ways to do so.

In computing, a hardware random number generator (HRNG) or true random number generator (TRNG) is a device that generates random numbers from a physical process, rather than by means of an algorithm. Such devices are often based on microscopic phenomena that generate low-level, statistically random "noise" signals, such as thermal noise, the photoelectric effect, involving a beam splitter, and other quantum phenomena.

Again even these phenomenons are not 100% Truly Random ( this is debatable though ), but the thousands of factors involved in their calculations make them almost impossible to predict. For measuring these factors, we require external specialised hardware for the same.

You can have a look at RANDOM.ORG that claims to offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
Here’s the link : https://random.org

There are some other methods as well like mouse movements, delay between keyboard presses etc.
You can also have a look at these generators, who claim to be Truly random :
The following (non-exhaustive) list of websites claim to provide random numbers generated from a truly random source

1) Australian National University : QRNG( quantum random number generator ) : https://qrng.anu.edu.au/
2) Humboldt University of Berlin : QRNG( quantum random number generator ) :
https://qrng.physik.hu-berlin.de/
3) HotBits : Generated by radioactive decay :
https://www.fourmilab.ch/hotbits/

You can read about the theory behind their generators on their websites, I am not including those here, as it will make this article quite complicated ( it might be already for some of you ).

Thanks for your patience !
If you found this article useful, Share this !
It took a lot of time and efforts for this one, and yes, this topic isn’t done yet, stay tuned for the next article on this series, that will focus on Cryptographic aspect of randomness.

Connect With Me :
Twitter
GitHub

And recently as a hobbie, I have started photography, you can have a look at that here :
Instagram

Discussion (4)

Collapse
lukeshiru profile image
Luke Shiru

Nowadays, in JS, if you need a safe random value for cryptography, ideally you should use the global crypto. Take a look here.

Collapse
prakhart111 profile image
Prakhar Tandon Author

Thanks, was unaware of this!

The documentation says :
Implementations are required to use a seed with enough entropy, like a system-level entropy source.
So by system level entropy, i think they are taking about key-press, mouse movements etc.

Collapse
jonrandy profile image
Jon Randy

It is impossible to get truly random numbers since true randomness is a concept, not a reality

Collapse
prakhart111 profile image
Prakhar Tandon Author • Edited on

Yes,
I also quoted this in the article :

"Again even these phenomenons are not 100% Truly Random (this is debatable though), but the thousands of factors involved in their calculations make them almost impossible to predict."

Things we are unable to predict, are so called "Random" to us, the day when we get capable to trace them or predict them, they won't be random anymore !