Computer Science 20 June 2026 5 min read

How computers generate random numbers

A republished post from my old 2017 blog explaining true random numbers, pseudo-random numbers, entropy, and why randomness matters in computing.

Randomness Programming Cryptography
Dice rolling on a table

I found this post from my old blog and wanted to bring it back here. It was originally published on September 16, 2017.

This was long before ChatGPT and the current wave of AI writing tools. I wrote it myself at the time, as a student trying to explain a computing concept in simple language. I am republishing it with light formatting, but keeping the original explanation and tone mostly intact.

Is it possible for a computer to have the ability to generate random numbers? Can computers have the property “randomness”?

We know computer functionality is all about code. If randomness is also gained by code, is it actually random? If it is code, should it have a pattern that can be predicted? So how do computers actually generate random numbers?

Before that, let’s see what random numbers are used for.

What random numbers are used for

Randomness, or random numbers, are used in very simple applications like gambling, rolling a dice, and flipping a coin. They are also used in complex applications like advanced games and cryptography.

It is one of the most essential needs when it comes to programming.

Aside from the simple applications, let’s consider cryptography. Cryptography needs random numbers, such as cipher keys, that cannot be guessed or predicted by hackers or attackers.

A word cloud showing random number use cases

How are they generated?

Random numbers generated by computers can be put into two types:

  • True random numbers
  • Pseudo-random numbers

True random numbers

To generate true random numbers, computers gather “entropy”, or random data, from a physical event happening outside the computer.

For example, a computer can listen to atmospheric noise. No one can predict that noise. At the same time, the random value generated by the computer using that noise also cannot be predicted by hackers or attackers.

If we consider a more day-to-day example, most of you, if you are a Linux user, may have experienced a request made by your computer to do some random things like typing on the keyboard, moving the mouse, using applications, and so on. This helps the computer generate random numbers for activities like generating keys.

A Linux dialog asking the user to generate random data for key generation

Humans are not predictable at all. So no one can predict the random numbers created from that kind of activity.

Pseudo-random numbers

Computers cannot always generate true random numbers, because it is costly and inefficient in performance.

To avoid this, pseudo-random numbers are generated using some kind of algorithm and a seed value. Of course, they are predictable. If someone knows the algorithm, predicting the numbers is not difficult.

But if we consider a computer game where random things should happen, it usually does not matter whether the numbers are true random or pseudo-random.

In cases like cryptography, however, pseudo-random numbers obviously cannot be used unless they come from a cryptographically secure generator designed for that purpose.