History & Technology

True Randomness vs Pseudo-Randomness: Why It Matters for Code Generation

Every programming language has a built-in way to generate a random number, and for most everyday purposes it works perfectly well. Shuffle a deck of cards in a game, pick a random background colour, roll a virtual die, none of it needs to survive an actual attack. The moment randomness protects something valuable though, a password, a gift card balance, a session that proves who you are, the everyday kind of random stops being good enough, and most people generating codes have never had a reason to learn why.

Quick answer

General-purpose randomness (like JavaScript's Math.random()) is a pseudo-random number generator, fast and statistically well distributed but theoretically predictable to someone who studies its internal state. Cryptographically secure randomness (like crypto.getRandomValues()) draws from unpredictable physical entropy sources and is specifically designed to resist prediction, which is what security-sensitive codes need.

What is pseudo-random number generation actually doing?

A pseudo-random number generator, the kind behind a typical Math.random() call, is a mathematical formula that produces a sequence of numbers which look statistically random, evenly distributed, no obvious pattern, but are entirely determined by an initial starting value called a seed. Feed the same seed into the same algorithm twice and you get the exact same sequence of "random" numbers both times. That's not a flaw for most applications, it's often a useful feature, letting a game replay the same random level from a shared seed, for instance. But it means that if an attacker can work out or guess the seed and the algorithm, the entire sequence becomes predictable.

Where does cryptographically secure randomness actually come from?

Cryptographically secure random number generation takes a fundamentally different approach. Rather than starting from a single seed and running a predictable formula, it draws from an entropy pool built up by the operating system out of genuinely hard-to-predict physical events: the precise microsecond timing between keystrokes, small variations in disk read times, mouse movement jitter, and on many modern processors, dedicated hardware that generates randomness from electrical noise. This entropy feeds a cryptographic pool that JavaScript's crypto.getRandomValues() and equivalent server-side functions draw from, and it's specifically engineered so that even someone who fully understands the algorithm cannot predict future output from past output.

Why can't you just tell by looking at the output?

This is the part that trips people up: a sequence produced by Math.random() and a sequence produced by crypto.getRandomValues() can look identically random to a human being staring at them. Both pass basic statistical randomness tests. The difference isn't in how the output looks, it's in whether a sophisticated attacker who studies the algorithm and gathers enough samples could, in principle, predict what comes next. For rolling dice in a mobile game, that theoretical predictability doesn't matter to anyone. For generating a password reset token, it matters enormously, because predictability is exactly what an attacker is trying to find.

Does this actually matter for the codes you generate day to day?

It depends entirely on what the code protects. A one-off code for a small office raffle or a fun giveaway carries essentially no risk even if it were generated with ordinary pseudo-random logic, because there's nothing meaningfully at stake if someone predicted it. A password, an API key, a session token, a gift card code representing real stored value, all of these deserve cryptographically secure randomness without question, because predictability there is a genuine security failure, not a theoretical curiosity.

How does this site generate codes?

The generator on this site uses the browser's built-in cryptographically secure random source rather than a general-purpose random function, for exactly the reasons above, and everything happens locally in your browser with nothing transmitted anywhere. Whether you're generating a password, an API key, or a batch of voucher codes, the underlying randomness is the same cryptographic quality regardless of what you're using it for.

Frequently Asked Questions

Is Math.random() safe to use for generating codes?

Not for anything security-sensitive. Math.random() in JavaScript is a pseudo-random number generator built for speed and general use, not cryptographic strength, and its output can theoretically be predicted if an attacker learns enough about its internal state.

What's the actual difference between crypto.getRandomValues() and Math.random()?

crypto.getRandomValues() pulls from the operating system's cryptographically secure random source, the same pool used to generate encryption keys, and is specifically designed to be unpredictable even to an attacker who knows the algorithm. Math.random() is optimised for speed and statistical distribution, not unpredictability against a determined attacker.

How does a computer generate randomness at all if it just follows instructions?

Modern operating systems gather entropy from physical, hard-to-predict sources: precise timing of mouse movements, keyboard presses, disk activity, and dedicated hardware random number generators on many chips. That entropy feeds a cryptographic pool that crypto.getRandomValues() draws from.

Does it matter for a one-off giveaway code if the randomness is only pseudo-random?

Practically, often not much, since the stakes of guessing a giveaway code are low. It matters far more for anything protecting real value or access: passwords, API keys, session tokens, gift card codes, where predictable randomness could be actively exploited.

How can I tell if a tool is using proper cryptographic randomness?

You generally can't tell from the output alone, both types of randomness can look equally unpredictable to a human. It comes down to trusting how the tool was built, whether it explicitly uses crypto.getRandomValues() or an equivalent server-side cryptographic source rather than a general-purpose random function.

Does a random seed make pseudo-random output secure?

Not on its own. Seeding a pseudo-random generator with something unpredictable, like the current time, narrows the space an attacker has to search, but it's still fundamentally a formula producing a deterministic sequence, which is a different security guarantee than a cryptographically secure source provides.

← Back to Guide