Room for ChanceThe science of chance

Random number generators

Cryptographically Secure Random Numbers: What the Claim Means

Learn why CSPRNGs concern unpredictability, why Math.random is different, and why a secure source still needs unbiased range conversion.

RoomForChance · 3 min read · Published · How this work was prepared

Cryptographically secure randomness concerns the difficulty of prediction under an explicit threat model. It is a stronger requirement than producing numbers that look mixed or roughly balanced. An application can pass a simple randomness test and still expose every future value to someone who understands its algorithm.

For a web application, the source and the surrounding code both matter. A cryptographic source does not automatically make every transformation of its output uniform, and an unbiased choice does not automatically make an entire system secure.

Why observing the output should not reveal the state Imagine a generator with a hidden counter that increases by one on every call. Printing a complicated transformation of that counter may disguise the pattern from casual inspection. But if an observer can recover the counter, the next output becomes predictable. Security analysis asks whether such an attack is practical, not whether a short sample looks appealing.

A CSPRNG is designed to resist relevant inference attacks. Its protection depends on initialization, state handling and implementation. It is not a promise that every possible operational mistake has been prevented.

The browser facility The Web Cryptography API defines crypto.getRandomValues for filling supported integer arrays with cryptographically strong random values. An application should use the documented facility directly when that property is required. JavaScript's Math.random is not an interchangeable claim of cryptographic security.

The W3C specification is the appropriate reference for the API contract. This article does not claim to audit a particular browser's operating-system entropy collection or certify every deployed device. Those are separate layers of the system.

One million outputs per method, using a deliberately small eight-bit source range. Modulo assigns four faces 43 byte values and two faces 42; rejection accepts 252 values divided equally among six faces. The bars show samples, the reference line shows 1/6.
Figure 1. One million outputs per method, using a deliberately small eight-bit source range. Modulo assigns four faces 43 byte values and two faces 42; rejection accepts 252 values divided equally among six faces. The bars show samples, the reference line shows 1/6.

Why range conversion still matters Suppose a program obtains a uniform 32-bit unsigned value and wants an integer from 0 through m−1. Simply taking value mod m produces a slight imbalance unless m divides 2^32. The counts of source values assigned to destinations differ.

A standard correction accepts values below L = floor(2^32/m) × m, rejects the rest, and returns the remainder of an accepted value. There are exactly L/m accepted source values for each destination. The mapping argument is mathematical and does not rely on the output looking random in a chart.

Distinct numbers and separate pools A lottery generator also needs to enforce the correct rules. Main numbers selected without replacement must be distinct. Special balls drawn from a separate pool must be generated independently of the main labels, except where the game's actual rules say otherwise. A visually plausible line can still be generated from an incorrect sample space.

What this changes for a lottery user Secure generation can protect the impartiality and unpredictability of your selection procedure. It does not make an external fair draw more likely to match your line. A manually chosen valid combination has the same jackpot probability as a generated valid combination under the uniform model.

Our Lab deliberately uses reproducible simulation seeds, which should not be confused with hidden security state. This separation makes both claims clearer: the experiment is reproducible, while a security-sensitive generator must satisfy its own unpredictability requirements.

Leave the selection to chance

If you want a valid random game line, open the relevant generator. A generated line is not an official entry or a prediction, and it does not improve the probability of a specified valid combination.

Sources and further reading

The worked examples and derivations are RoomForChance explanations. Operator sources establish game parameters; research sources support the specific points identified above. University links are references, not endorsements.

  1. W3C · Web Cryptography Level 2API specification; the linked Level 2 document is a working draft. The source contract and the application mapping are separate layers.
  2. Joe Blitzstein and Jessica Hwang · Harvard Stat 110 / Introduction to ProbabilityUniversity-level further reading on counting, conditioning and probability models.

Continue the argument

Modulo Bias Explained: Why a Random Byte Makes an Unfair Die
See the exact 256-to-6 mapping behind modulo bias, calculate the imbalance, and understand how rejection sampling removes it.

Rejection Sampling: How Discarding Values Can Preserve Fairness
A practical derivation of rejection sampling for integer ranges and distinct lottery numbers, with efficiency and failure cases explained.

True Random vs Pseudorandom: Which Difference Matters?
Compare physical entropy and deterministic random streams without confusing reproducibility, statistical quality and cryptographic security.