Core answer: A fair random number between 1 and N gives every value probability 1/N. Computers don't produce true randomness — they use pseudo-random number generators (PRNGs) seeded by entropy; for games that's fine, for cryptography you need CSPRNGs (like crypto.getRandomValues). Rolling two dice is NOT uniform: 7 appears 6/36 of the time, 2 and 12 only 1/36.
Uniform vs not: the dice table
| Sum of 2d6 | Ways | Probability |
|---|---|---|
| 2 / 12 | 1 | 2.8% |
| 3 / 11 | 2 | 5.6% |
| 4 / 10 | 3 | 8.3% |
| 5 / 9 | 4 | 11.1% |
| 6 / 8 | 5 | 13.9% |
| 7 | 6 | 16.7% |
Sums of independent uniforms converge to a bell curve (Central Limit Theorem) — that is why board games balance around 7.
True random vs pseudo-random
- PRNG (Math.random, rand()): deterministic sequence from a seed; fast; fine for shuffles, games, simulations. Never for keys or tokens.
- CSPRNG (crypto.getRandomValues, /dev/urandom): seeded from OS entropy (timings, hardware noise); unpredictable even with full state knowledge of a PRNG.
- Physical: dice, coin flips, atmospheric noise, quantum shot noise — true randomness, but slow and bias-prone in practice (a slightly worn die is measurably unfair over 10,000 rolls).
Worked examples
Example 1 — Shuffle fairness. The Fisher–Yates shuffle picks a uniform swap partner for each position in one pass; naive "sort by random key" or repeated swap-anywhere methods measurably bias orderings.
Example 2 — Sampling without replacement. Drawing 3 winners from 100 entries: P(any specific person picked) = 3/100, but P(all 3 specific) = 1/C(100,3) = 1/161,700.
Example 3 — The birthday paradox. Only 23 people make P(shared birthday) > 50%; at 60 people it is 99.4%. Intuition fails because pairs grow quadratically: C(23,2) = 253 pairs.
Common mistakes and myths
- "Due" numbers — a fair die that missed 6 for 20 rolls is still 1/6 next roll; no memory exists (gambler's fallacy).
- Modulo bias — rand() % 10 is slightly unfair when the generator's range is not a multiple of 10; use rejection sampling for correctness.
- Using Math.random for security tokens — PRNG state can be reconstructed from outputs; session IDs need CSPRNG.
- Confusing unlikely with rigged — a 1-in-a-million event happens ~8,000 times a day worldwide at population scale.
- Testing randomness by eye — humans see patterns in noise (a fair coin yields streaks of 5+ regularly); use chi-square tests, not vibes.