Web Crypto and unbiased random integers

Understand how the browser's cryptographic random words become uniformly distributed integers.

A 32-bit random word has 2³² possible values. Applying a remainder directly to a range whose size does not divide 2³² creates a small bias. The engine instead calculates the largest multiple of the range size that fits and rejects values outside it.

Consider a toy random source with eight possible values, 0–7, and three target values. Taking a remainder gives counts 3, 3, 2; discarding 6 and 7 first leaves counts 2, 2, 2. The production engine uses the same rejection principle with 32-bit words.

To try it, generate numbers between 1 and 6 with duplicates allowed. Uniformity means equal probabilities, not equal counts in every short run. Web Crypto supplies the random words; our range mapping preserves uniformity under the assumption that the words are uniform.

Related tools

References

LHTSOFT Random