How Online Coin Flips and Dice Rolls Stay Fair: Modulo Bias, Streaks and Odds
Fair randomness has three traps: modulo bias in the code, streaks that look broken but are not, and the belief that the next flip is due. We measured all three.
Where the Randomness Comes From
Browsers offer two random number sources. Math.random is fast but not designed to be unpredictable, and its output depends on the browser’s implementation. crypto.getRandomValues fills an array with values from a cryptographically secure generator, the same kind used for encryption keys. Our Coin Flipper and Dice Roller use only the second. A coin needs one bit, so one random byte gives eight independent flips.
The Remainder Trap: Modulo Bias
The obvious way to turn a random byte (0 to 255) into a die roll is to take the remainder after dividing by 6 and add 1. That is quietly unfair, because 256 is not a multiple of 6: four faces get 43 of the 256 byte values and two faces get only 42.
| Method | Chance of faces 1-4 | Chance of faces 5-6 |
|---|---|---|
| Random byte % 6 | 43/256 = 16.797% | 42/256 = 16.406% |
| 32-bit value % 6 | about 16.6667% each | differs by about 1 in 700 million |
| Rejection sampling (ours) | exactly 1/6 | exactly 1/6 |
The difference is small for one roll and real over thousands; with a 20-sided die and a byte, some faces come up 13/256 (5.08%) of the time and others 12/256 (4.69%). Larger random values shrink the bias without removing it. Rejection sampling removes it: draw a 32-bit value, and if it lands in the leftover slice at the top (values that would favor some faces) throw it away and draw again. For a d7 that happens about once in a billion draws, so it costs nothing.
Streaks Are Normal
People expect a fair coin to alternate more than it does. In 100 flips the longest run of the same side is most often 6 or 7, and the chances are higher than intuition suggests:
| Flips | Longest streak of | Chance |
|---|---|---|
| 100 | 7 or more | 54% |
| 100 | 10 or more | 8.7% |
| 1,000 | 10 or more | 62% |
| 1,000 | 15 or more | 3.0% |
While testing the coin flipper, one of our 1,000-flip batches produced 18 tails in a row, a roughly 0.4% event. It looked broken; it was not. Two million more flips in the same browser matched every expected statistic.
The Gambler’s Fallacy
After five heads, tails is not “due”. Each flip is independent, so the next one is still 50/50. We checked this directly on 10 million flips: the four pairs HH, HT, TH and TT each appeared a quarter of the time, so knowing one flip told us nothing about the next.
Dice Odds Worth Knowing
Dice totals are not equally likely even when each die is fair. Three six-sided dice total 10 with probability 1/8 but 3 or 18 with 1/216 each. Rolling four dice and dropping the lowest (written 4d6kh3) averages 12.2446 instead of 10.5. Rolling two twenty-sided dice and keeping the higher, which tabletop games call advantage, raises the chance of 15 or more from 30% to 51%. The Dice Roller computes the exact odds for any expression you type.
Are Real Coins Fair?
Heads and tails, yes; starting side, not quite. In a preregistered study of 350,757 tosses by 48 people, Bartoš and colleagues found coins landed on the side they started 50.8% of the time, as a physics model by Diaconis and colleagues had predicted, while heads came up 50.0% overall. A digital flip has no starting side.
How We Checked
All figures here come from tests on the tools’ own code: 10 million coin flips and 600,000 rolls per die type checked with chi-square tests, dice distributions compared with exact enumeration in Python, and streak probabilities calculated exactly. A deliberately broken version of each tool failed those tests.
Flip or roll
Fair coin flips and dice with exact odds.
Frequently Asked Questions
Is Math.random good enough for a coin flip?
What is modulo bias?
How long a streak is normal in 100 coin flips?
After several heads, is tails more likely?
What does advantage do to a d20 roll?
Published Oct 14, 2026 by Derek Giordano
Browse all utility tools →