DEV Community

Saurabh Sharma
Saurabh Sharma

Posted on

Explain how a Random number is generated Like I'm Five

what really happens when we put Math.random()

I have always listened that random numbers are not truly random, they are something pseudo-random.

Why true random number can't be generated?

Discussion (2)

nektro profile image
Meghan (she/her) • Edited on

Why true random number can't be generated?

Because computers are designed solely to do simple task over and over again in a very predictable matter. But that's exactly the problem. It's not random if it predictable.

What we make instead is called a pseudorandom number generator. These are wild algorithms that generate "random" outputs. However, they are still algorithms and with the same input will always have the same output. To combat this, many PRNGs "seed" the algorithm with almost unpredictable data like the time or the angle of the device from the gyro.

Quantum processors bring about the potential for "truly" random number generators, but we don't really know because we don't know a lot about quantum physics, and it depends on the knowability of sub-atomic information.

kspeakman profile image
Kasey Speakman

To add to what @nektro said, there are pseudorandom number generators (PRNG) and cryptographically secure RNGs (CSRNG).

The main difference being in non-CS RNGs, you can usually spot a repeating pattern. See this article. (Took me forever to find that again.)

The only way to get really random numbers is to have a random input. For instance, uses atmospheric whitenoise measurements as its random input.

On computers without access to physical phenomena the OS collects whatever "noise" it has access to and uses that. For instance /dev/random on linux uses timings from device drivers, keyboard, and other stuff as its random input.