Back to the blog

Random

How random number generators work.

What a computer's random numbers are, why a browser's cryptographic generator is fair for draws and games, and how a range is drawn without bias.

By 5 min read

The Random number generator showing 42 in large figures, two dice showing four and three, and a coin showing heads

A computer follows instructions, so how can it pick a number nobody can predict? The answer is a generator seeded with real unpredictability, and a careful way of turning its output into the range you ask for.

Where the randomness comes from.

Your browser's cryptographic generator is seeded by the operating system with unpredictable events from the hardware, and then stretched by an algorithm made so that nobody without the seed can tell its output from true chance. It is the same generator that makes the keys for secure websites.

Some services measure physical noise, such as RANDOM.ORG's atmospheric noise. For picking a number, rolling a die or shuffling cards, a cryptographic generator is just as unpredictable to everyone involved.

Turning it into 1 to 100 without bias.

The generator gives whole numbers from 0 to 4,294,967,295. Taking the remainder after dividing by 100 would make the low numbers very slightly more likely, because 4,294,967,296 is not a multiple of 100. The fix is rejection: draw again whenever the number falls past the last whole multiple. KeepSimp does this for every range, so every number is exactly as likely.

With or without repeats.

No repeats is on by default, like drawing from a hat: each number comes up at most once. Turn it off and every number has the same chance each time, so repeats happen; in 23 numbers from 1 to 365, two match more often than not.

Decimals work the same way: with two places, 0.00 to 1.00 is 101 values, each as likely.

Further reading

Topics: random number generator · how do random number generators work · true random vs pseudo random · random number 1-100 · rng · unbiased random numbers

A little less theory. A little more doing.

Try it on your own terms, with a free browser tool.

Try the tool