top of page

# How Computers Generate Random Numbers

The other day, I had to make a very important decision between ice cream and cake, one that I’d typically rather leave up to chance. So what do I do when faced with these tough choices? I flip a coin. When I don’t happen to have a coin nearby, I have the computer to do it for me. It returns a result arbitrarily: either heads or tails. But a machine only does what it’s programmed to do, so how can a computer generate a random value?

There are actually two ways: true random number generators (TRNGs) and pseudo-random number generators (PRNGs).

Before we jump in, it’s important to acknowledge the significance of random number generation beyond helping me choose my dessert. Computational RNG first came to fruition in the 1940s and 50s, and has since been implemented in countless realms such as gambling and cryptography. RNG also promotes equity; because randomness has no bias, it ensures that all groups of people or objects involved have equal opportunity to be chosen.

### True random numbers

The first method of random number generation involves monitoring conditions outside the computer that have entropy, meaning they demonstrate unpredictable patterns.

Let’s take a look at some examples of environmental factors used by TRNGs:

• Atmospheric noise: globally, lightning strikes about 40 times per second. By monitoring the static produced by these lighting strikes, random numbers can effectively be generated.

• Radioactive decay: there is no way of predicting when the nucleus of an atom will decay, making it an effective source of entropy.

• Time: by taking the exact time down to nanoseconds that a mouse is clicked or a key pressed (e.g. 10:52:07.348796724 AM), the computer can use the last digit (in this case 4) to produce a random number.

When it comes to generating complex strands of numbers, TRNGs make it nearly impossible for cyber-attackers to predict the sequence.

### Pseudo-random numbers

Rather than relying on external behaviour to generate values, PRNGs use pre-set algorithms to form a sequence of numbers. Pseudo-random number generators are periodic; this means that after a certain number of iterations, the sequence of numbers generated will repeat. A good PRNG should have a long period.

There are two main components to PRNGs: the seed and the algorithm. The seed is a number that the user inputs into the system. The generator then transforms this seed into another number using a pre-set algorithm. The new value then becomes the seed for the next iteration, and the process repeats until the desired length is reached.

The most classic example of a PRNG is the linear congruence algorithm. It follows this formula:

Xn+1 = a(Xn) + c mod m

Where a, c, and m are all positive constants, X0 is the seed value, and the following conditions are met: m > a, m > c, and m > X0. Notice how X0 is transformed into X1 by the mathematical operations provided by a, c, and m; in the next iteration, X1 becomes the new seed to generate X2.

### Comparing the two generators

TRNGs and PRNGs vary in their level of security and efficiency.

When it comes to encryption, TRNGs are generally the safer bet because of the PRNGs’ deterministic properties. PRNGs use a pre-set algorithm and seed value; since each number in the sequence relies on previous ones, a hacker would only need to know one value and the algorithm to crack the code.

Unlike TRNGs, PRNGs also behave periodically; the sequence will eventually repeat itself. However, this is generally not a huge concern because countless combinations of seed and algorithm have a period that is long enough to be practical.

A noteworthy downfall to TRNGs is often their inaccessibility. Believe it or not, finding and using the technology necessary to monitor radioactive decay is both expensive and inefficient. There is also possibility for wear and tear of the instruments. This is not a problem for PRNGs, as their algorithms are invulnerable to external conditions.

If you don’t want people to be able to guess your numbers, true number generators are most suitable. But when it comes to choosing my dessert, I think I’ll stick with a pseudo-random number generator — or maybe I’ll just stick to flipping regular coins.

Works Cited

Arobelidze, Alexander. “Random Number Generator: How Do Computers Generate Random Numbers?” FreeCodeCamp.org, FreeCodeCamp.org, 26 Oct. 2020, www.freecodecamp.org/news/random-number-generator/.

“Can a Computer Generate a Truly Random Number?” BBC Science Focus Magazine, www.sciencefocus.com/future-technology/can-a-computer-generate-a-truly-random-number/.

Haahr, Mads. “True Random Number Service.” RANDOM.ORG - Introduction to Randomness and Random Numbers, www.random.org/randomness/.

Hoffman, Chris. “How Computers Generate Random Numbers.” How-To Geek, 5 Nov. 2019, www.howtogeek.com/183051/htg-explains-how-computers-generate-random-numbers/.

Lynch, Peter. “Random Numbers Plucked from the Atmosphere.” The Irish Times, 4 Dec. 2018, www.irishtimes.com/news/science/random-numbers-plucked-from-the-atmosphere-1.3714968.

bottom of page