Predictably Unpredictable

Every computer follows rules, so where does randomness come from? From Python's algorithms to the mysteries of quantum mechanics, discover whether computers can ever escape determinism.

St Andrews 20266 min read · 3 reads

Ever opened a loot box in a video game, hoping to get that one rare item, or wondered how a game decides which rewards you receive? Behind every seemingly random drop is a computer executing a series of instructions. But this raises an interesting question: if computers operate according to carefully defined rules and algorithms, can they ever produce something truly random? After all, how can a machine that follows instructions generate an outcome that, by definition, cannot be predicted?

The Illusion of Chance

To understand this apparent contradiction, we first need to consider determinism. At its simplest, determinism is the principle that events are determined by preceding conditions: if the initial state of a system and the rules governing it are known, its subsequent behaviour can theoretically be predicted. Computers are built around this principle. Every operation, from performing a simple calculation to rendering complex graphics, follows instructions encoded in software and executed by hardware. Given the same initial conditions, inputs and internal state, a deterministic computer will produce exactly the same output every time. This is what makes computers so reliable: they do not arbitrarily return different answers to the same mathematical equation. However, this presents a problem when generating randomness. If a computer's behaviour is determined by its initial state and the instructions it follows, then surely every number it generates must also be predetermined. What we perceive as random may simply be the result of processes whose underlying mechanisms we cannot see.

Image accompanying Predictably Unpredictable

So how do computers generate numbers that appear random? One widely used approach involves pseudorandom number generators (PRNGs), algorithms that produce sequences which imitate the statistical properties of randomness. Python's built-in random module, for example, uses an algorithm called the Mersenne Twister, developed by Makoto Matsumoto and Takuji Nishimura. It begins with a seed: an initial value from which the entire sequence is determined. In Python, we can specify one using random.seed(42). The algorithm uses this seed to initialise an internal state, which it repeatedly updates through mathematical and bitwise operations, including XOR and bit shifting, to generate new values. Its period, the number of outputs before the sequence repeats, is an extraordinary 2^19937 - 1, a number with over 6,000 digits. This makes it useful for simulations, where long sequences without easily detectable patterns are desirable. Yet the Mersenne Twister is entirely deterministic: give it the same seed and it will reproduce precisely the same sequence. Its complexity does not make it genuinely random; it simply makes its output convincing. This reveals an important distinction: a sequence can exhibit the statistical properties of randomness without actually being generated by a random process.

But how can we determine whether a sequence behaves randomly? In mathematics and statistics, randomness is investigated through measurable properties. One is uniform distribution, which describes how evenly outcomes are spread across a range. If a fair six-sided die is rolled many times, for example, we expect each face to appear approximately one-sixth of the time. This is related to the law of large numbers: as the number of independent trials increases, the observed relative frequency of an outcome tends towards its theoretical probability. However, an even distribution alone is insufficient. Consider the sequence 1, 2, 3, 4, 5, 6, repeated indefinitely. Every number appears equally often, yet the pattern is completely predictable. We must also consider statistical independence: the occurrence of one outcome should not influence the probability of the next. With independent fair coin tosses, for instance, ten consecutive heads do not make tails any more likely on the eleventh toss. Statistical tests can identify potential biases and patterns by examining frequencies, runs, correlations and other properties. The Mersenne Twister performs well against many such tests, which helps explain its usefulness in simulations. However, passing statistical tests does not prove that a sequence is truly random. Such tests can only detect patterns they are designed to identify, meaning a deterministic sequence can pass them while remaining entirely predictable. If algorithms cannot produce fundamentally random outcomes, perhaps computers can obtain randomness from the physical world instead.

Image accompanying Predictably Unpredictable

Beyond Algorithms

This leads to true random number generators (TRNGs), which use physical processes rather than relying solely on a predetermined mathematical sequence. They obtain entropy, a measure of uncertainty, from sources such as thermal noise: tiny electrical fluctuations caused by the random motion of electrons inside components. Sensors can measure these fluctuations and convert them into binary data. However, raw measurements are not necessarily suitable for direct use. Electrical interference, environmental conditions and imperfections in equipment can introduce biases or correlations into the data. TRNGs therefore often use randomness extraction to process raw measurements and reduce these unwanted patterns, producing output that is closer to a uniform distribution. Unlike a conventional PRNG, a hardware generator can continually collect fresh entropy from its surroundings rather than simply updating a fixed internal state. This provides a way of introducing physical unpredictability into a computer. Nevertheless, the output still needs to be processed and assessed carefully: a physical source does not automatically guarantee high-quality random numbers. In some applications, however, statistical quality alone is not enough. What matters is whether anyone can predict the numbers being generated.

This distinction is especially important in cryptography, where the security of computer systems can depend on unpredictable numbers. Encryption uses cryptographic keys, sequences of bits that determine how information is protected. If an attacker can guess or predict a key, even a sophisticated encryption algorithm may be compromised. This is why the Mersenne Twister is unsuitable for security-sensitive applications. Although it performs well in statistical tests, it was not designed to resist deliberate attempts at prediction. If enough of its output is observed, its internal state can be reconstructed, allowing future values to be predicted. Cryptographic systems instead use cryptographically secure pseudorandom number generators (CSPRNGs). These are designed so that, even if someone observes previous outputs, predicting future ones remains computationally infeasible. They may use cryptographic techniques based on algorithms such as AES or ChaCha20, while operating systems can seed and refresh them using entropy gathered from hardware and other sources. Crucially, a CSPRNG does not need every output to originate directly from a physical random event. It needs a sufficiently unpredictable starting state and an algorithm that makes its outputs practically impossible to predict. Statistical randomness concerns the patterns visible in a sequence, whereas cryptographic security concerns whether an adversary can exploit those patterns or predict what comes next. The two are related, but they are not the same.

Image accompanying Predictably Unpredictable

But Is It Truly Random?

So, the next time you open a loot box and receive that elusive legendary item, spare a thought for the mathematics behind your apparent stroke of luck. The computer generating that outcome may not produce randomness in quite the same way we intuitively understand it: its algorithms follow mathematical rules, its hardware can extract entropy from physical phenomena, and its cryptographic systems can produce outputs that are practically impossible to predict. Yet perhaps this distinction matters less than it initially appears. After all, if a sequence is statistically indistinguishable from randomness and computationally impossible to predict, does it matter whether it was ever truly random to begin with? Computers may not need to recreate the uncertainty of the physical world; they simply need to convince us that they have. Although that legendary loot box item would still be nice.

Reader discussion

Join the conversation.

0 contributions

No comments yet. Start the discussion.

Deep Dive

Written by students at Eton.
Read by anyone curious.

Your cookie preferences

We use essential cookies to run this website. With your permission, we also use analytics cookies to understand how visitors use the site.