DEV Community

Cover image for Why Your Computer Can't Just Pick a Number: Navigating the Spectrum of Randomness
Doogal Simpson
Doogal Simpson

Posted on • Originally published at doogal.dev

Why Your Computer Can't Just Pick a Number: Navigating the Spectrum of Randomness

TL;DR: Computers are deterministic, meaning they struggle to create "true" randomness. I solve this using a spectrum of techniques: Pseudo-Random Number Generators (PRNGs) for logic like gaming, hardware-based True Random Number Generators (TRNGs) for standard security, and quantum systems for absolute cryptographic unpredictability where physics guarantees the result.

One of the best things about computers is that they do exactly what you tell them. And one of the worst things about computers is that they will do exactly what you tell them. If I want a random number, I’m immediately running into a wall because machines are deterministic by design. They don't "guess"; they calculate.

When I need a random result, I can just flip a coin or roll a die. It’s messy, physical, and easy. But for a computer, providing a random value requires breaking its own internal logic to find a source of chaos. Depending on the stakes—whether I'm building a loot drop for an RPG or a high-level encryption layer—I have to choose the right level of randomness.

Why is generating a random number so hard for a computer?

Computers are deterministic systems, meaning if I give a machine the same input and state, it will produce the exact same output every single time. Because a computer lacks the natural "messiness" of a human, it cannot generate a truly random value without an external source of noise.

I’ve found that many engineers overlook how rigid our hardware really is. Every operation is the result of a defined instruction set. If I ask a function to return a random value, that function has to execute logic. And if that logic is based on math, it’s reproducible. To get something that feels random, I have to point the computer at something it can't control.

What is a pseudo random number generator and when should I use one?

A Pseudo-Random Number Generator (PRNG) is a deterministic algorithm that takes a starting "seed" and runs it through a formula to produce a sequence of numbers that appear random. While the output looks chaotic to a user, the entire sequence is actually fixed and will repeat perfectly if I use the same seed twice.

I use PRNGs for the vast majority of my work—specifically in areas like video games or UI testing. If I’m building a game like Minecraft, I actually want this determinism; it’s what allows players to share a "world seed" and see the exact same terrain. For standard tasks like calling Math.random(), a PRNG is plenty, but I have to remember that if an attacker knows my seed, they can predict every "random" number that follows.

How does a true random number generator harvest physical entropy?

True Random Number Generators (TRNGs) move beyond algorithms by harvesting entropy from physical chaos within the hardware, such as CPU temperature fluctuations or the nanosecond timing of hardware interrupts. Instead of calculating a number, the system is essentially "measuring" the noise of the physical world.

I’ve seen people point to simple system time as a source of truth, but let’s be clear: system time is usually just a seed for a PRNG. To get to the TRNG level, I’m looking for hardware "jitter." These are the tiny, unpredictable micro-fluctuations in thermal noise or the exact moment a packet hits a network card. This is the standard for things like gambling websites, where I need to ensure that no amount of reverse-engineering can reveal a pattern in the deck shuffle.

Level of Randomness Source Predictability Best Use Case
Pseudo (PRNG) Seeded Algorithms High (if seed is known) Games, UI, Simulations
True (TRNG) Hardware Entropy (Heat/Jitter) Very Low Gambling, SSL Certificates
Quantum (QRNG) Subatomic Particles Zero High-Stakes Cryptography

Do I need quantum randomness for secure cryptography?

Quantum randomness is the gold standard used when the stakes are high enough that I need unpredictability guaranteed by the laws of physics. This involves measuring events at the subatomic level—like sending particles at a half-silvered mirror—where the outcome is a literal 50/50 probability.

In the world of cryptographics, "good enough" usually isn't enough. If there is even a slight statistical bias in my random number source, a sophisticated attacker can exploit it to break an encryption key. By reaching into the realm of quantum mechanics, I ensure that the randomness is genuine and absolute. It moves the security of the system from a software challenge to a physical certainty.

FAQ

Is the random number generator in my programming language secure?
Generally, no. Most default functions like Math.random() or rand() are PRNGs designed for speed, not security. If I’m generating a password or a session token, I always reach for a cryptographically secure library like the Web Crypto API or crypto/rand in Go.

What happens if I use the same seed in a PRNG?
I will get the exact same sequence of "random" numbers every time. This is a common pitfall in testing; if I don't vary my seed (often by using the current timestamp), my "random" tests will actually be testing the exact same path over and over.

Where does a headless server get its entropy if there’s no user input?
Modern servers gather entropy from hardware sources like the RDRAND instruction on Intel CPUs or interrupt timings from the disk and network. If a system runs out of this entropy, it can actually "starve," causing processes that require high-quality randomness to hang until more chaos is harvested.

Top comments (0)