Randomness & Entropy
Transcript
Made automatically from the video's sound, so it may contain mistakes. Click a line to play from there.
Right, let's dive straight into this explainer, where we're going to completely shatter the illusion that randomness is just a simple function call. I mean, if you've ever typed out a quick command to generate a random token or maybe shuffle an array, you might actually be shocked by the sheer mathematical complexity operating just under the surface.
Innocent one-liners can seal unforgiving machine invariants that LLMs routinely violate with Happy Path defaults. It's just a hard truth of computing, really. A completely deterministic Von Neumann architecture, by its very design, cannot create true randomness on its own.
It literally cannot roll dice. So when AI coding agents or unwary developers rely on those happy path defaults, they frequently violate these invariants, and that results in absolutely critical security flaws.
Section one, harvesting physical chaos. So to force a rigid deterministic CPU to act unpredictably, we actually have to look outside the CPU entirely. And the journey from the physical world into cryptographic security is honestly wild.
These machines harvest physical chaos. We're talking interrupt arrival jitter, thermal noise, and famously, Cloudflare's Lava RAN setup, which is quite literally a wall of lava lamps over in San Francisco. All this chaotic input gets stirred together into an entropy pool.
Then the modern Linux kernel uses algorithms like Blake 2S for entropy extraction and Chacha 20 for fast key erasure. That gives us backtracking and prediction resistance. It's a phenomenal pipeline, specifically designed to turn messy reality into pure unpredictable bits.
Section 2. Debunking the Uranor myth. Now, once we've harvested all that physical chaos, we have to navigate some dangerous decades-old folklore.
For years, developers were told that devslash random was the better choice for true security, and devslash urandom was just a less secure fallback. Well, that's completely false. Both actually feed from the exact same cryptographically secure pseudo-random number generator, or CSPRNG.
The only real difference? Dev slash random blocks unpredictably when it estimates entropy is low, which can literally freeze your systems up. For almost all cryptographic purposes, devslash urandom isn't just preferred, it's absolutely sufficient.
There is, however, one major exception. Think about virtual machines or routers that suffer from boot time seed starvation, especially right after they've been cloned. They might desperately request random numbers before the kernel has had time to gather enough physical chaos.
That's where getRandom2 comes in. Introduced in Linux 3.17, it is the modern solution. It blocks only once, just to safely gather that initial entropy at early boot, and then it never blocks again.
Section 3. Cloning PRNG internal states. So what happens when we just rely on standard built-in maths functions instead? In a word, betrayal, mathematical betrayal.
Take the number 624. If you're using the wildly popular MERSEN twister, MT1933. By merely observing exactly 624 consecutive 32-bit outputs, an algebraic attacker can completely invert the tempering transform.
From just that handful of outputs, they can clone the entire 19,937-bit future state of your generator. Every single random number it produces from then on is now entirely predictable to the attacker.
And if we look at standard PRNGs across the board, you'll see that even if a generator passes rigorous statistical test suites, it offers zero cryptographic defense against a dedicated attacker. For instance, the V8 JavaScript engine updated math.random to use XOR Shift128 Plus.
It passes statistical tests brilliantly, but as the engineers themselves explicitly noted, it is absolutely not cryptographically secure. The same goes for linear congruential generators. They are trivial to predict.
Now, Melissa O'Neill's PCG family offers much better statistical performance and definitely makes prediction challenging. But again, standard PRNGs are structurally vulnerable to algebraic attacks. Section 4.
Because of these vulnerabilities, history is a terrifying graveyard of cryptographic keys completely ruined by bad randomness. Back in 1996, Netscape's SSL was broken, simply because the seed relied on predictable process IDs and clocks.
Then in 2008, Debian maintainers commented out two lines of open SSL code that were flagged by a memory checker. That tiny mistake accidentally collapsed the pool of global SSH and TLS keys down to a mere 32,768 process IDs.
We've also seen the 1999 ASF online poker hack caused by a weak 32-bit PRNG, the infamous Russian iPhone slot machine syndicates, and a massive 2012 disaster where routers booted up with identical states, causing mass cryptographic key collisions. And of course, we absolutely cannot forget dual EC DRBGB.
This was the algorithm heavily pushed into standards by the NSA. Because the P and Q mathematical C points weren't truly random, but specifically chosen by the creators, an adversary could predict your entire SSL clear text handshake from just 32 bytes of output.
It was literally an elliptic curve cleptographic backdoor built entirely on rigged randomness. Section 5: Shuffling and Modulo Bias. Now have you ever seen code that sorts an array using math.random minus 0.5.
It is fundamentally broken. A naive sort provides n to the power of n paths, so for three items that's 27 paths. But it's trying to cover a factorial number of permutations, which would be six. Since 27 doesn't divide evenly by six, it results in horribly biased shuffles.
To put it into perspective, for a six-card deck, there are 720 possible permutations, but the sorting path simply don't map to them evenly. Some cards will reliably end up in certain positions way more often than others. We actually see very similar flaws with the modulo operator.
When you use RAND, modulo 52 to pick a card, it doesn't evenly divide the maximum value of a 32-bit integer. The lower numbers end up getting a slight fractional bias. And as your range increases, this bias scales linearly, up to 50%.
Daniel Lemir's rejection sampling fixes this, not by squeezing the numbers, but by safely rejecting out-of-range bounds entirely, which perfectly preserves true mathematical randomness. Section 6.
Escaping the vibe coder trap. This brings us to a crucial playbook for AI coding agents, because they frequently fall right into what we call the vibe coder trap. Generating a token with math.random in JavaScript or random.choice in Python passes every single unit test.
Because a string like K8F29A looks securely scrambled to human eyes. The vibes are good, right? But as we've just learned, it relies on a predictable PRNG. It is utterly transparent to a machine, leaving your application completely exposed to attackers.
So your main takeaway from this explainer is to enforce a strict split in your code base. Mandate true OS C SPRNGs, like crypto.getrandom values in JavaScript or the Secrets module in Python for absolutely anything adversarial or security-facing.
If you need to shuffle, you use an unbiased Durstenfeld Fisher-Yates algorithm. And you reserve custom PRNGs like isolated cedable PCG instances exclusively for property tests, simulations, and procedural game worlds, where deterministic replay is actually a required feature.
You really have to respect the strict invariance of deterministic machines. But, you know, that's just the beginning of our journey into hard problems in computing. Next time, we are going to unpack the distributed systems nightmare that is idem potency and the elusive myth of exactly once delivery.
I mean, if you can't even trust your CPU to pick a random number, how on earth can you trust a network to charge a credit card exactly one time? I'll see you in the next explainer.