# Dice fairness: the method

Written and committed on 24 November 2026, **before** any full run, so that the
tests, sample sizes, significance level and correction were fixed before any result
was seen. A change to any of them gets a new suite version, a dated note at the end
of this file, and a fresh run. A failed run is not re-run to get a pass; if it is repeated, both reports are
published (see "When a release run fails").

## The question

Is every number Tablelight rolls fair: each face equally likely, each roll
independent of the others, and every combination of dice (sums, advantage,
keep and drop, exploding dice, Fate dice, percentile) coming out with exactly the
odds the rules say?

## What decides a roll

Every roll Tablelight makes is decided once, on the DM's device (an iPhone, iPad,
Mac or Windows PC), by one roller, written in Swift, that every version of
Tablelight shares. Android phones join only as players, so they never decide a
result at the table; the suite runs on Android too because the same roller and
Android's random source run there. The DM may change one of their own hidden rolls
before it settles (Out of scope, below).

- The roller has one place where a face is chosen. Every roll from dice notation
  (which covers advantage and disadvantage, keep and drop, exploding dice,
  percentile dice, Fate dice, death saves and the DM's secret rolls), a Fate
  reroll after an invoke, and creature initiative and its tie-breaks all go
  through it.
- It maps a random 64-bit word to a face with the Swift standard library's
  `Int.random(in:using:)`, which uses Lemire's multiply-and-reject method: the face
  is the high word of *word × faces*, and a word is drawn again when the low word
  lands in the short stretch that would favour some faces. Every face is exactly
  equally likely; there is no modulo bias. The check `range-mapping` confirms,
  word for word, that the roller does exactly this.
- At the table the words come from `SystemRandomNumberGenerator`, the operating
  system's cryptographic random source: `arc4random_buf` on Apple devices,
  `BCryptGenRandom` on Windows (and `getrandom` on Android). That source differs
  by platform, so the suite runs on each.
- The 3D throw never chooses a number. The result is decided first and the throw
  is planned to land on it; the check `throw-lands-on-result` confirms it.
- An automated check fails if any line of the app's code draws a random number
  (or rolls, or makes a seeded generator) and isn't on a reviewed list, so no new
  or changed path can choose a result some other way unnoticed.

## Hypotheses and the decision rule

Each test's null hypothesis is that the dice are fair and independent. A test
**fails** when it rejects that hypothesis.

- **Significance level:** α = 0.01 for the whole family: if the dice are fair, the
  chance that *any* test in a report fails is about 1%. "About", because the
  p-values come from large-sample (asymptotic) distributions; in the sparsest
  cells (expected counts near 5, in some exact and gap tests) the χ² tail is a
  little anti-conservative at Holm's smallest thresholds, which makes a false
  alarm slightly *more* likely, never a biased die harder to catch.
- **Correction:** Holm–Bonferroni across every test in the report (122 in suite
  version 1). Sort the p-values from smallest; the smallest is compared with
  α/122, the next with α/121, and so on; once one is not rejected, none after it
  is. Holm is valid whatever the dependence between tests (several tests here use
  the same rolls). Each test's threshold and adjusted p-value is in the report.
- **A run passes** when no test fails and every exact check passes. Every test is
  reported, pass or fail.
- **"Too good" results:** a χ² p-value near 1 means counts closer to perfect than
  chance allows. The batch tests catch this: their p-values must be spread evenly
  between 0 and 1, so a cluster near 1 fails as surely as a cluster near 0.

## Generators and seeds

- **System** (`--generator system`): what the table uses. This is the run that is
  published for each platform. It cannot be repeated roll for roll (that's the
  point of a cryptographic source). Every count is in the report, so every
  statistic built on counts can be recomputed from it; serial correlation and runs
  give only their summary figures (the sums and run counts), so those two can be
  checked from the figures, and recomputed from the rolls only in the seeded run.
- **Seeded** (`--generator seeded --seed 20261124`): a seeded generator
  (SplitMix64) through the same roller. Each stream's seed is FNV-1a-64 of
  `"<seed>:<stream>"` (for example `"20261124:main:1d20"`), so anyone can repeat
  the run, or any one stream, exactly. `reference.py`
  re-implements SplitMix64, the face mapping and the roller in plain Python and
  reproduces the seeded counts.
- **Reproducibility digests:** every report fingerprints 100,000 seeded rolls of
  each die and of 4d6dl1, 2d20kh1 and 1d6! (FNV-1a-64 of each total and a new
  line). They must be identical on every platform, and `reference.py` recomputes
  them.

## Sample sizes

| Tests | Rolls |
|---|---|
| Each die's main sequence (d2, d4, d6, d8, d10, d12, d20, d100, dF) | 10,000,000 |
| The face choice alone for d20 (creature initiative) and d1000 (tie-break) | 10,000,000 each |
| Each die's batches | 1,000 batches of 10,000 (another 10,000,000) |
| Each exact distribution and within-roll test | 1,000,000 rolls of the notation |

About 220 million rolls in all. Power, at the corrected level: with 10 million
rolls, Pearson's test finds a d6 with one face 0.43% more likely than it should be
(1/6 × 1.0043), a d20 with one face 1% more likely, or a d100 with one face 3%
more likely, nine times in ten. The smallest expected count in any tested cell is
5 (see pooling); for the plain dice it is far higher (100,000 per face for the
d100, 500 per cell for the d100's pairs).

## The tests

**Uniformity** (per die, on its main sequence):

1. Pearson's χ² goodness of fit, k − 1 degrees of freedom.
2. The G-test (likelihood ratio, G = 2 Σ O ln(O/E)), k − 1 degrees of freedom.
3. For the d100, which is thrown as two ten-sided dice: the tens digit and the
   units digit, each Pearson χ² with 9 degrees of freedom (100 shows 0 and 0).
4. The roller's face choice called directly, as creature initiative (d20) and its
   tie-break (d1000) call it: Pearson and G.

**Batches** (per die): 1,000 fresh batches of 10,000 rolls, each tested with
Pearson's χ²; the 1,000 p-values tested against the uniform distribution with a
two-sided one-sample Kolmogorov–Smirnov test (Stephens' correction:
p = Q_KS((√n + 0.12 + 0.11/√n)·D)). For the d2 and dF a batch's χ² p-value can take
only a few values, with a small atom at 1 (about 0.8% of batches for the d2); that
moves D far less than the test's threshold.

**Exact distributions** (1,000,000 rolls each, through the notation roller as the
table rolls them): 2d6, 3d6, 8d6, 4d6dl1 and 4d6kh3 (ability scores, both
spellings), 2d20kh1 (advantage), 2d20kl1 (disadvantage), 5d8kl2, 4d10dh1, 1d6!,
2d6! and 1d20! (exploding; the exact odds stop at the roller's cap of 20 explosions
per die, as the roller does; exploding dice with keep or drop aren't tested, since
the roller judges each exploded die on its own there), 4dF, and 4dF+1 rerolled by an invoke. The exact
probabilities are worked out independently of the roller, by convolution or by
listing every outcome (and recomputed separately by `check_report.py`). Pearson's χ², with neighbouring totals pooled until every
cell expects at least 5 rolls (left to right; a short last group joins the one
before); degrees of freedom = cells − 1.

**Independence** (per die, on its main sequence):

1. Serial correlation at lags 1 to 5: r = Σ(xᵢ − x̄)(xᵢ₊ₗ − x̄) / Σ(xᵢ − x̄)²,
   z = r√n, two-sided normal p-value.
2. Pairs: consecutive rolls taken two at a time without overlap
   ((x₀, x₁), (x₂, x₃), …), every one of the k² pairs equally likely, Pearson χ²
   with k² − 1 degrees of freedom.
3. Runs above and below the middle ((lowest + highest)/2; a dF's blank, which is
   the middle, is left out): Wald–Wolfowitz z, two-sided.
4. Gaps between top faces (a natural 20 on the d20): the number of rolls between
   one top face and the next is geometric, P(g) = p(1 − p)^g with p = 1/k; cells
   g = 0, 1, … up to the last one expecting 5, then "that many or more"; pooled as
   above; Pearson χ².

**Within one roll** (1,000,000 rolls each):

1. 2d6 and 2d20: the two dice of each roll, every pair of faces equally likely
   (Pearson χ², 35 and 399 degrees of freedom).
2. 8d6: each of the eight positions equally likely to show each face; one Pearson
   χ² per position, summed (40 degrees of freedom).

**Exact checks** (pass or fail outright, outside the statistical family):

- `range-mapping`: the roller's face choice matches a written-out copy of Lemire's
  method word for word, for 11 ranges (dF, d2 to d20, d100, d1000), 100,000 draws each.
- `d100-digits`: the d100's two thrown dice show each of the 100 results as its own
  pair.
- `explosion-cap`: a die that always rolls its top face stops after 20 explosions.
- `throw-lands-on-result`: 500 seeded rolls of one of every die shape (d4, d6, d8,
  d10, d12, d20, the d100's two dice and dF), planned by the app's throw planner
  as fresh throws on felt: every planned die comes to rest with the decided face's
  label up in the die's geometry. It doesn't cover carried-on or flicked dice, a seat's facing, special landings (the volcano's plate) or the
  numerals each app paints on its textures.

## Out of scope

- **Real dice and Bluetooth smart dice.** Players and the DM may roll their own
  dice and type the faces in, or roll Pixels dice that report their faces.
  Tablelight records those faces; it doesn't roll them, so their fairness is the dice's own.
- **The DM's changes.** The DM may change one of their own hidden rolls before it
  settles (never a player's or a guest's roll). The game then uses the DM's value;
  players see a changed roll's total only, never dice or a natural 20, or only "The
  DM rolled in secret", and the original stays on the DM's device. That is the DM's
  call, not the dice's.
- **The 3D throw**, which never chooses a number (above).
- **Debug and demonstration modes** used while building the app, which aren't part
  of the shipping apps' table.

## When a release run fails

A failed run blocks the release (decided 25 November 2026). The failure is
investigated, not re-rolled away: re-running until a run passes would make α meaningless. If the investigation finds no fault, the run is
repeated once, with both reports kept and published.

## Changes

- Suite version 1, 24 November 2026: first version.
- 24 November 2026, wording only, after the first full runs (no test, size, level
  or correction changed): what decides a roll (Android never decides one at the
  table; the DM may change their own hidden rolls), the family-wise level called
  "about" 1% and why, what can be recomputed from a system run, the throw check's
  reach, the d2 and dF batch atom, exploding dice with keep or drop, and the
  re-run rule above. The Windows runs were made again from the same build, because
  the first reports recorded more about the PC than we publish (not because of any
  result); both passed.
- 25 November 2026: a failed release run blocks the release (above). Reports name the
  platform generically only (its operating system, processor type and random source);
  the 24 November reports' platform details were cut back to that after the runs (so
  they're no longer byte for byte what the binary wrote); nothing else in them
  changed then.
- 25 November 2026, wording only: the names of the app's internal code, files and
  tools were taken out of this method and of the reports' descriptive text (the
  roller's face choice, the seeded generator, the method's file name, the debug
  modes). No number, test, statistic or verdict changed.
