Guides › Text & developer
Is your random picker really fair? Modulo bias, measured on 6.2 million draws
When a raffle, a classroom picker or a password generator says it is random, people assume every outcome is equally likely. Whether that is true depends on one small step that is easy to get wrong: turning the random bits a computer produces into a number in the range you want. The usual shortcut, the remainder operation (modulo), quietly favours some outcomes. We measured how much.
Short answer: taking a random byte (0–255) modulo 62 to pick a letter or digit made eight characters 25% more likely than the other 54 in our 6.2-million-draw test. The fix, called rejection sampling, discards the few values that cause the imbalance. All our random tools, including the number generator, wheel and password generator, use it.
Where the bias comes from
A byte has 256 possible values. Suppose you want one of 62 characters (A–Z, a–z, 0–9) and compute byte % 62. 256 is not a multiple of 62: it is 4 × 62 + 8. So the remainders 0 to 7 can each be produced by five different bytes, while remainders 8 to 61 can be produced by only four. The first eight results come up five times for every four of the rest. In general, whenever the size of the random source is not an exact multiple of the range, the lowest values get one extra chance each.
| Range (byte % n) | Values with an extra chance | How much more likely |
|---|---|---|
| Die, n = 6 | 4 of 6 | 2.4% |
| Digit, n = 10 | 6 of 10 | 4% |
| Letter, n = 26 | 22 of 26 | 11% |
| Letter or digit, n = 62 | 8 of 62 | 25% |
| Percentile, n = 100 | 56 of 100 | 50% |
The measurement
We drew 6.2 million bytes from the operating system's cryptographic random generator (Python's secrets module) and mapped each one to a 62-character alphabet in two ways. Theory says each character should appear about 100,000 times.
- Modulo shortcut: the eight favoured characters (A to H) averaged about 121,000 appearances each and the other 54 about 96,900, a ratio of 1.25, exactly as the arithmetic predicts. "A" appeared 21% more often than an unbiased picker would give it. The source bytes were perfectly good; the bias came entirely from the mapping step.
- Rejection sampling: we threw away every byte of 248 or more (the largest multiple of 62 that fits in 256 is 248) and used
byte % 62on the rest. 96.9% of bytes were kept, and every character landed within 1% of the expected count, which is the normal statistical scatter for a sample of this size.
Does it matter in practice?
It depends on how big the random source is compared with the range. With a single byte, as above, the bias is large. Good implementations draw 32 or 53 random bits instead, which shrinks the effect enormously: for a 32-bit number modulo 100 the favoured values are more likely by about 0.000002%. That is harmless for a classroom picker, but it is still a bias, and for passwords, cryptographic keys, lotteries or anything where people could lose money, the correct approach costs almost nothing. Our tools use 32-bit draws plus rejection sampling, and 53-bit draws for very large ranges, so the result is exactly uniform.
Math.random() or crypto.getRandomValues()?
Browsers offer two sources of randomness. Math.random() is fast and fine for games and animations, but its output is not designed to be unpredictable; MDN explicitly says not to use it for anything security-related. crypto.getRandomValues() draws from the operating system's cryptographic generator and is the right choice for passwords, tokens and draws that someone might want to dispute. All our random-pick tools (the number generator, wheel, password generator and UUID generator) use the cryptographic source.
Three other ways "random" tools go wrong
- Shuffling by sorting with a random comparison. Code like
list.sort(() => Math.random() - 0.5)does not produce a fair shuffle; some orders come up far more often, depending on the browser's sorting algorithm. The standard fair method is the Fisher–Yates shuffle, which our number generator uses for unique draws from ranges of up to 100,000 numbers. - Rounding a random decimal.
Math.round(Math.random() * 10)gives 0 and 10 only half as often as 1 to 9, because each end value collects only half a unit of the range. UseMath.flooron a range that is one larger. - Re-drawing until it "looks random". Removing results because they seem unlikely, such as the same name twice in a row, makes the outcome less random, not more. If repeats are not allowed, draw without replacement from the start.
How to test a picker yourself
Generate a large batch, for example 1,000 numbers from 1 to 10 with our random number generator, and count how often each value appears. Each should be near 100. Expect natural variation of roughly ±30; a value that is consistently 20% high across several batches points to a biased tool. For a quick visual check, paste the list into a spreadsheet and make a histogram.
Our own generator, tested from its screen
To check our random number generator the way a visitor uses it, we entered each setting on its page in Chromium 153, ran Generate repeatedly, and counted the numbers shown on screen.
- 1 to 10, 100,000 numbers: every value appeared between 9,891 and 10,175 times, against 10,000 expected. A chi-square test gave p ≈ 0.47, which is ordinary chance variation.
- 1 to 6, 60,000 numbers (a die): each face came up between 9,897 and 10,083 times, p ≈ 0.86.
- 1 to 45, six numbers with no repeats, 2,000 draws: no draw contained the same number twice, and each number appeared between 228 and 314 times against about 267 expected, p ≈ 0.17.
Separately, we repeated the byte experiment described above, outside the tool, on another 6.2 million bytes. With the modulo shortcut the eight favoured characters averaged 121,149 hits against 96,867 for the other 54. With rejection, 96.87% of bytes were kept and all 62 characters landed between 96,294 and 97,407, within 1% of the 96,872 expected.
You can download every count we used. Each file is plain CSV with a header row, so it opens in any spreadsheet:
- Counts from the generator, 1 to 10 and 1 to 6: columns range, value, count and expected.
- 2,000 six-number draws from 1 to 45: one row per draw, columns draw and n1 to n6, as shown on screen.
- 6.2 million bytes mapped to 62 characters: columns character, modulo_count and rejection_count.
Common questions
Is a computer's random number truly random?
Cryptographic generators are seeded from unpredictable physical events in the hardware and are then expanded by algorithms designed so that no one can predict the output. For every practical purpose, including security, they are treated as random.
Can I reproduce a draw later?
Not with a cryptographic generator, which is the point. If you need a draw others can verify, record the result at the time with witnesses or a screenshot showing the time.
Is the wheel fair if it lands on the same name twice?
Yes. With independent draws, repeats are expected. With ten names, the chance that a given spin repeats the previous winner is 10%.
References
- MDN: Crypto.getRandomValues()
- MDN: Math.random() (not suitable for security)
- Daniel Lemire (2019), Fast Random Integer Generation in an Interval, ACM TOMACS
- Python documentation: secrets module
Written and checked by Ria Kim for GlowHalo; figures without a source link come from the steps shown on this page. Companion tool: Random Number Generator. Spotted an error? Write to help@glowhalo.org. Chromium is a trademark of its owner; GlowHalo is not affiliated with it.