9.3 Uniform, normal, integer and shuffle algorithms
The core generator is SplitMix64. Each next_u64 increments the 64-bit state by the golden-ratio constant and applies two xor/shift/multiply mixing rounds followed by a final xor shift. Arithmetic wraps modulo (2^{64}).
Uniform values
The uniform generator takes the upper 53 random bits, adds 0.5 and divides by (2^{53}). Values lie on the open interval (0,1), so downstream logarithms in Box–Muller never receive zero.
Normal values
Standard normals use the cosine form of Box–Muller:
One call consumes two uniform values and returns one normal. This consumption pattern is part of reproducibility: replacing Box–Muller with a faster algorithm would change every subsequent draw and requires an RNG derivation/version decision.
Uniform integers
Sampling from [0,n) uses Lemire's multiply-high method with rejection below a threshold, eliminating modulo bias. n=0 returns zero defensively. Fisher–Yates shuffle draws one unbiased index for each decreasing suffix.
Validation
Test exact known sequences for fixed (seed,label,version) inside the implementation, then test distributional properties separately. Sequence tests detect accidental algorithm changes; moment/uniformity tests detect broad statistical defects. Neither replaces the other.
Avoid documenting only empirical histograms. A reproducible generator needs a precise algorithm, consumption order and version boundary.
Common mistakes
- Using
% nfor integer sampling and introducing modulo bias. - Assuming two Box–Muller implementations consume the same stream.
- Treating distributional tests as exact sequence tests.
Checklist
- Freeze known vectors for each derivation version.
- Verify open-interval uniform endpoints.
- Test shuffle permutations and integer frequencies.
Further reading
See antithetic/quasi-random draws, checkpoint compatibility and Chapter 42's planned release gates.