Skip to content

12.3 Modular arithmetic, hashing, and PCG32 in Python

Once you start training models, the system makes random choices everywhere: initial weights, dropout masks, the order of training windows, the token the sampler picks. Every one of them has to be reproducible from one seed. Otherwise a failing run cannot be replayed, a resumed checkpoint diverges from the run it continues, and a parity test between the Python sampler and the Rust engine of Candle model runner, Rust KV cache, sampler and PCG32 has nothing to compare.

Concepts from other sections (9)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

numpy’s generator, Rust’s rand, and Go’s math/rand all produce different streams for the same seed. So the books fix one generator to the bit, PCG32, specified in spec/pcg32.md, and this section implements it in Python for training. Published vectors give the later Rust and Go ports the same oracle. The same mathematics, arithmetic modulo a power of two, also gives the system the hash functions it needs: FNV-1a for KV block hashes and universal hashing for hash tables.

The solution is rng.py, which provides PCG32 (next_u32, uniform, uniforms, below, shuffle, substream, state, set_state) together with splitmix64, child_seed, fnv1a64, and universal_hash; its contract is rng.pyi. No earlier code is needed. The counting arguments come from the Discrete Math 1 problem set, and if you want to practise modular arithmetic and hashing by hand first, work through the first problem set of this chapter.

Learning objectives

  • Compute in Z264\mathbb{Z}_{2^{64}} in Python, where integers never overflow, and explain why every step needs an explicit mask.
  • Derive PCG32 from a linear congruential generator, and compute its first output for seed 0 by hand.
  • Build a uniform double with all 53 bits from integer arithmetic alone, and draw unbiased bounded integers by rejection.
  • Prove that the Carter-Wegman family is universal, and explain why FNV-1a chains over buffers.
  • Derive independent sub-streams from one seed with SplitMix64 so that one consumer’s draws never change another’s.

The notation for the section is gathered here first.

SymbolMeaningType
a mod na \bmod nthe remainder of aa divided by nn, in {0,…,n−1}\{0, \dots, n-1\}integer
Zn\mathbb{Z}_nthe integers {0,…,n−1}\{0, \dots, n-1\} with ++ and ×\times taken mod nn
sts_tgenerator state after tt stepsu64
AAthe PCG multiplier 6364136223846793005u64
ccthe increment, inc =2⋅seq+1= 2 \cdot \mathit{seq} + 1, always oddu64
rotr32(x,k)\mathrm{rotr}_{32}(x, k)rotate the 32-bit xx right by kk bitsu32
uua uniform draw in [0,1)[0, 1)double
h,m,ph, m, pa hash function, its number of buckets, a prime
⊕\oplusbitwise exclusive or (^)

Every integer aa and modulus n≥1n \ge 1 give a unique quotient and remainder, a=qn+ra = qn + r with 0≤r<n0 \le r < n; write r=a mod nr = a \bmod n. Two integers are congruent mod nn when they have the same remainder. Remainders respect addition and multiplication: (a+b) mod n=((a mod n)+(b mod n)) mod n(a + b) \bmod n = ((a \bmod n) + (b \bmod n)) \bmod n, and the same holds for ×\times. So you may reduce at every step instead of at the end, and Zn\mathbb{Z}_n is a closed number system.

A uint64_t in C holds exactly Z264\mathbb{Z}_{2^{64}}: the C standard defines unsigned overflow as wraparound, which is reduction mod 2642^{64}. Python integers never overflow, so x * y of two 64-bit values has up to 128 bits. The solution reduces with & (2**64 - 1), since keeping the low 64 bits is the remainder mod 2642^{64}. Forget it once and the state grows without bound while the outputs, which read the low bits, go quietly wrong after the first wrap.

Two facts about Z2k\mathbb{Z}_{2^k} carry the rest of the section. An odd number has a multiplicative inverse mod 2k2^k, so multiplying by an odd constant is a bijection: it scrambles without losing information. And x↦x⊕(x≫k)x \mapsto x \oplus (x \gg k) is a bijection too, because the top kk bits pass through unchanged and determine how to undo the rest.

The first use of this arithmetic is hashing. A hash function maps keys to bucket numbers, h:U→{0,…,m−1}h: U \to \{0, \dots, m-1\}. Two keys collide when h(x)=h(y)h(x) = h(y). With nn keys in mm buckets the load factor is α=n/m\alpha = n/m, and if collisions are as rare as for random assignment, a lookup scans about 1+α1 + \alpha keys.

No single fixed function is good for every set of keys, since an adversary can pick keys that all collide. A universal family instead picks hh at random so that for any two different keys, Pr⁡[h(x)=h(y)]≤1/m\Pr[h(x) = h(y)] \le 1/m. Carter and Wegman’s family works like this: choose a prime pp larger than every key, draw a∈{1,…,p−1}a \in \{1, \dots, p-1\} and b∈{0,…,p−1}b \in \{0, \dots, p-1\}, and set

ha,b(x)=((ax+b) mod p) mod m.h_{a,b}(x) = ((a x + b) \bmod p) \bmod m .

Here is why it works. For x≠yx \ne y, the map (a,b)↦(r,s)=(ax+b mod p, ay+b mod p)(a, b) \mapsto (r, s) = (ax + b \bmod p,\ ay + b \bmod p) is a bijection onto the pairs with r≠sr \ne s, because x−yx - y has an inverse mod the prime pp, so a=(r−s)(x−y)−1a = (r - s)(x - y)^{-1} and then bb are determined. A collision needs r≡s(modm)r \equiv s \pmod m; for each rr, at most ⌈p/m⌉−1≤(p−1)/m\lceil p/m \rceil - 1 \le (p-1)/m values s≠rs \ne r qualify. That gives at most p(p−1)/mp(p-1)/m colliding pairs out of p(p−1)p(p-1), a probability of at most 1/m1/m. The order of the two reductions matters: ((ax+b) mod m) mod p((ax + b) \bmod m) \bmod p is a different function and loses the guarantee.

FNV-1a is a fast non-cryptographic hash of a byte string. Start from the 64-bit offset basis h0=0xCBF29CE484222325h_0 = \texttt{0xCBF29CE484222325}, and for each byte bb do h←(h⊕b)⋅P mod 264h \leftarrow (h \oplus b) \cdot P \bmod 2^{64} with the prime P=0x100000001B3P = \texttt{0x100000001B3}. Xor first, then multiply: that order is the “1a” (FNV-1 multiplies first, and hashes differently). Because the state between bytes is just hh, hashing a buffer in two pieces, the second starting from the first’s result, equals hashing it at once: fnv(b,fnv(a))=fnv(a∥b)\mathrm{fnv}(b, \mathrm{fnv}(a)) = \mathrm{fnv}(a \| b). The Paged KV block pool uses exactly that to chain each KV block’s hash onto its parent’s.

Hashing scrambles a key once. A generator scrambles its own state over and over, and the simplest way to do that is a linear congruential generator (LCG), which steps st+1=(Ast+c) mod 264s_{t+1} = (A s_t + c) \bmod 2^{64}. With cc odd and A≡1(mod4)A \equiv 1 \pmod 4 it visits all 2642^{64} states before repeating (the Hull-Dobell theorem). Its weakness is the low bits: bit kk of sts_t depends only on bits 0..k0..k of the previous state, so it repeats with period 2k+12^{k+1}, and the lowest bit simply alternates. An LCG’s raw low bits are not random at all.

PCG keeps the LCG for the state and outputs a permutation of the old state that uses only its good high bits:

next_u32():
old = state
state = old * A + inc (mod 2^64)
xs = u32(((old >> 18) XOR old) >> 27) (xorshift: high bits fold into the middle)
rot = old >> 59 (the top 5 bits, 0 to 31)
return rotr32(xs, rot) (a rotation chosen by the state itself)

Taking the output from old lets C compute the multiply and the permutation in parallel. inc selects one of 2632^{63} streams: inc = (seq << 1) | 1, forced odd, because an even increment breaks the full period. Seeding is O’Neill’s pcg32_srandom_r(seed, seq): state = 0, one step, state += seed, one step. The two steps push the seed through the multiplier so that seeds 0 and 1 do not give nearly identical first outputs.

Warning: In C, the rotation has one trap. xs << (32 - rot) with rot = 0 shifts a 32-bit value by 32, which is undefined behavior: x86 and ARM happen to compute something, the optimizer may compute something else, and UBSan stops the program. Write the left shift as xs << ((0u - rot) & 31u). For rot = 0 it shifts by 0, and xs | xs is still xs.

The generator produces 32-bit integers, but most consumers want a number in [0,1)[0, 1). A double has a 53-bit significand, so the finest grid of evenly spaced doubles in [0,1)[0, 1) is k⋅2−53k \cdot 2^{-53} for k=0,…,253−1k = 0, \dots, 2^{53} - 1. One 32-bit draw is not enough bits, so the spec uses two:

u=(⌊a/25⌋⋅226+⌊b/26⌋)⋅2−53,u = \left(\lfloor a / 2^5 \rfloor \cdot 2^{26} + \lfloor b / 2^6 \rfloor\right) \cdot 2^{-53},

taking 27 bits from aa and 26 from bb. The integer in the parentheses is below 2532^{53}, so the double holds it exactly, and multiplying by a power of two is exact too. No rounding happens anywhere, which is why uniform() is bit-identical in every language, and why u<1u < 1 always. Inverse-CDF sampling in Sampling and logit processors relies on never seeing 1.0. In C, compute in uint64_t: (a << 26) on a 32-bit type loses the top bits.

The other common request is an integer in [0,n)[0, n). Drawing it from rr uniform on [0,232)[0, 2^{32}) as r % n is biased whenever nn does not divide 2322^{32}: the first 232 mod n2^{32} \bmod n residues get one extra preimage. For n=3⋅230n = 3 \cdot 2^{30} the values below 2302^{30} come up half the time instead of a third.

The fix is rejection. Let t=232 mod nt = 2^{32} \bmod n, computed as (2**32 - n) % n so it fits in 32 bits; draw until r≥tr \ge t and return r mod nr \bmod n. The accepted range has 232−t2^{32} - t values, a multiple of nn, so every residue is equally likely, and fewer than half of the draws are ever rejected.

With unbiased bounded integers you can shuffle. Fisher-Yates shuffles a list uniformly: for i=n−1i = n-1 down to 11, pick jj uniformly in [0,i][0, i] and swap positions ii and jj. Each of the n!n! sequences of choices gives a different permutation, so all are equally likely. Picking jj in [0,i)[0, i) instead (Sattolo’s algorithm) never leaves an element in place and produces only the (n−1)!(n-1)! cyclic permutations.

One user seed must feed several independent consumers (initialization, dropout, shuffling, sampling), so that turning dropout on does not change the initial weights. The spec derives each purpose’s generator from the seed alone:

child_seed(s,p)=mix64(s+p⋅0x9E3779B97F4A7C15),stream(s,purpose)=PCG32(child_seed(s,p),seq=p),\mathrm{child\_seed}(s, p) = \mathrm{mix64}(s + p \cdot \texttt{0x9E3779B97F4A7C15}), \qquad \mathrm{stream}(s, \mathit{purpose}) = \mathrm{PCG32}(\mathrm{child\_seed}(s, p), \mathit{seq} = p),

with purpose ids init 1, dropout 2, shuffle 3, sample 4, mutation 5. mix64 is SplitMix64’s output function, two rounds of xorshift and odd multiply, both of them bijections as shown earlier:

z = (z XOR (z >> 30)) * 0xBF58476D1CE4E5B9
z = (z XOR (z >> 27)) * 0x94D049BB133111EB
return z XOR (z >> 31) (all mod 2^64)

A SplitMix64 generator seeded with ss adds the golden-ratio constant to its state and mixes, so its kk-th output is mix64(s+k⋅0x9E37...)\mathrm{mix64}(s + k \cdot \texttt{0x9E37...}). The child seed of purpose pp is therefore simply output pp of SplitMix64 seeded with ss. It depends on the seed, never on how many draws the parent made.

Start with the first output of pcg32(0), the spec’s worked_example_seed_0. Seeding with seed = 0, seq = 54 goes like this:

StepComputationResult
inc(54≪1)∣1=108+1(54 \ll 1) \mid 1 = 108 + 1109
first step0⋅A+1090 \cdot A + 109state = 109
add the seed109+0109 + 0109
second step109⋅A+109 mod 264109 \cdot A + 109 \bmod 2^{64}state = 0x9AE4F7499BA72696

Now call next_u32() with old = 0x9AE4F7499BA72696. The table follows the permutation one quantity at a time.

QuantityValue
old >> 180x000026B93DD266E9
(old >> 18) XOR old0x9AE4D1F0A675407F
... >> 27, low 32 bitsxs = 0x5C9A3E14
rot = old >> 5919 (the top 5 bits, 10011)
rotr32(xs, 19) = (xs >> 19) | (xs << 13) (low 32 bits)0x47C28B93 = 1203932051

That is the first entry of next_u32["0"] in spec/pcg32.vectors.json. The second draw is 0xB98F6A27, so the first uniform() uses ⌊0x47C28B93/32⌋=37622876\lfloor \texttt{0x47C28B93} / 32 \rfloor = 37622876 and ⌊0xB98F6A27/64⌋=48643496\lfloor \texttt{0xB98F6A27} / 64 \rfloor = 48643496, giving (37622876⋅226+48643496)⋅2−53=2524828517416360⋅2−53=0.2803122753265841(37622876 \cdot 2^{26} + 48643496) \cdot 2^{-53} = 2524828517416360 \cdot 2^{-53} = 0.2803122753265841.

FNV-1a of "a" is shorter. "a" is the byte 0x61. h=0xCBF29CE484222325⊕0x61=0xCBF29CE484222344h = \texttt{0xCBF29CE484222325} \oplus \texttt{0x61} = \texttt{0xCBF29CE484222344}, then h⋅0x100000001B3 mod 264=0xAF63DC4C8601EC8Ch \cdot \texttt{0x100000001B3} \bmod 2^{64} = \texttt{0xAF63DC4C8601EC8C}. The full product is 0xCBF29CE5DEAF63DC4C8601EC8C; keep the low 16 hex digits.

Finally, a universal hash with a=3a = 3, b=7b = 7, p=13p = 13, m=5m = 5, x=10x = 10: 3⋅10+7=373 \cdot 10 + 7 = 37; 37 mod 13=1137 \bmod 13 = 11; 11 mod 5=111 \bmod 5 = \mathbf{1}.

The module exposes one generator class and four free functions:

# tinyllm/num/rng.py (contract: rng.pyi)
class PCG32:
def __init__(self, seed: int, seq: int = 54) -> None: ...
def next_u32(self) -> int: ...
def uniform(self) -> float: ...
def uniforms(self, n: int) -> NDArray: ...
def below(self, n: int) -> int: ...
def shuffle(self, xs: MutableSequence[T]) -> None: ...
def substream(self, purpose: str) -> "PCG32": ...
def state(self) -> tuple[int, int]: ...
def set_state(self, s: tuple[int, int]) -> None: ...
def splitmix64(x: int) -> int: ...
def child_seed(seed: int, purpose_id: int) -> int: ...
def fnv1a64(data: bytes, h: int = 0xCBF29CE484222325) -> int: ...
def universal_hash(x: int, a: int, b: int, p: int, m: int) -> int: ...

state and set_state exist so a checkpoint can store the generator; fnv1a64 takes an optional starting hash so that buffers chain.

The reasoning that leads to the code runs as follows.

  1. Every random choice must replay from one seed in Python, Rust, and Go. So the generator cannot be a library’s; it must be a fixed algorithm specified to the bit, with published vectors: PCG32 as in spec/pcg32.md.
  2. The state lives in Z264\mathbb{Z}_{2^{64}} and outputs in Z232\mathbb{Z}_{2^{32}}. C gets the reduction from uint64_t overflow; Python ints never overflow, so every multiply and add that can exceed 64 bits is followed by & MASK64, and every 32-bit output by & MASK32.
  3. The raw LCG has weak low bits, so the output is a permutation of the old state built from its high bits: an xorshift, then a rotation by the top 5 bits. The increment is forced odd, and seeding follows O’Neill’s two-step procedure exactly, or no vector matches.
  4. Anything built on top must also be bit-exact. A uniform double therefore uses only integer arithmetic and one exact scaling by 2−532^{-53}; a bounded integer uses rejection, which is exact and unbiased; the shuffle is Fisher-Yates with j∈[0,i]j \in [0, i].
  5. Independent purposes need independent generators that depend only on the seed. So a sub-stream is a fresh PCG32 seeded by child_seed(seed, purpose), never derived from the parent’s current state.
  6. The hashes are the same arithmetic: FNV-1a is xor then multiply mod 2642^{64}, taking an optional starting value so buffers chain; the universal hash reduces mod pp before mod mm.

The constants are the spec’s, named once:

MASK64 = (1 << 64) - 1 # x & MASK64 == x mod 2^64
MASK32 = (1 << 32) - 1
MULT = 6364136223846793005 # the PCG multiplier A (odd, A = 1 mod 4)
GOLDEN = 0x9E3779B97F4A7C15 # SplitMix64's increment
FNV_OFFSET = 0xCBF29CE484222325
FNV_PRIME = 0x100000001B3
PURPOSES = {"init": 1, "dropout": 2, "shuffle": 3, "sample": 4, "mutation": 5}

Seeding is pcg32_srandom_r(seed, seq): state 0, one step, add the seed, one step. The hand trace followed it for seed 0, landing on state 0x9AE4F7499BA72696:

class PCG32:
def __init__(self, seed: int, seq: int = 54) -> None:
self.seed = seed & MASK64 # kept: substream derives children from it
self._state = 0
# inc must be odd: an LCG mod 2^64 has full period only with an odd
# increment. seq picks one of 2^63 distinct streams.
self._inc = ((seq << 1) | 1) & MASK64
self.next_u32() # state = inc
self._state = (self._state + self.seed) & MASK64
self.next_u32() # push the seed through the multiplier

next_u32 advances the state and permutes the old one. The rotation is written with (-rot) & 31 as the left-shift amount, the same expression a C port needs to avoid shifting by 32 when rot = 0:

def next_u32(self) -> int:
old = self._state
self._state = (old * MULT + self._inc) & MASK64 # reduce: Python ints grow
# Output from the OLD state: xorshift high bits down, keep 32 bits...
xs = (((old >> 18) ^ old) >> 27) & MASK32
rot = old >> 59 # ...rotate by the top 5 bits
return ((xs >> rot) | (xs << ((-rot) & 31))) & MASK32

uniform builds a 53-bit integer from 27 + 26 bits and scales it by 2−532^{-53}. Both steps are exact in a double, so every language gets the same bits and the result is at most 1−2−531 - 2^{-53}:

def uniform(self) -> float:
a = self.next_u32() >> 5 # 27 bits
b = self.next_u32() >> 6 # 26 bits
# a * 2^26 + b is an integer below 2^53, exact in a double, and the
# product with 2^-53 is exact too: no rounding anywhere.
return (a * 67108864 + b) * (1.0 / 9007199254740992.0)

below computes the rejection threshold t=232 mod nt = 2^{32} \bmod n as (2**32 - n) % n, which is the same residue, and redraws until r≥tr \ge t. shuffle is Fisher-Yates from the top down:

def below(self, n: int) -> int:
if not 1 <= n <= 1 << 32:
raise ValueError(f"below: n must be in [1, 2^32], got {n}")
# 2^32 mod n raw values would map to the low residues one extra
# time; rejecting r < t leaves a multiple of n values, each residue
# equally often.
t = ((1 << 32) - n) % n
while True:
r = self.next_u32()
if r >= t:
return r % n
def shuffle(self, xs: MutableSequence[T]) -> None:
for i in range(len(xs) - 1, 0, -1):
j = self.below(i + 1) # 0 <= j <= i: xs[i] may stay put
xs[i], xs[j] = xs[j], xs[i]

A sub-stream is a new generator whose seed and stream id depend only on the parent’s seed and the purpose, so draws already made on the parent cannot change it. state and set_state expose exactly the two words a checkpoint needs, and set_state refuses an even increment, which no PCG32 can have:

def substream(self, purpose: str) -> "PCG32":
p = PURPOSES[purpose]
return PCG32(child_seed(self.seed, p), seq=p)
def state(self) -> tuple[int, int]:
return (self._state, self._inc)
def set_state(self, s: tuple[int, int]) -> None:
state, inc = int(s[0]), int(s[1])
if inc % 2 == 0: # a corrupt checkpoint, not a PCG32 state
raise ValueError(f"set_state: inc must be odd, got {inc}")
self._state = state & MASK64
self._inc = inc & MASK64

SplitMix64’s mixer is two xorshift-multiply rounds and a final xorshift, each a bijection of 64-bit words, and child_seed is SplitMix64’s output number purpose_id for that seed:

def splitmix64(x: int) -> int:
z = x & MASK64
z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & MASK64
z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & MASK64
return z ^ (z >> 31)
def child_seed(seed: int, purpose_id: int) -> int:
return splitmix64((seed + purpose_id * GOLDEN) & MASK64)

Last come the two hashes. fnv1a64 takes the previous hash as its starting value, which is what makes fnv1a64(b, fnv1a64(a)) == fnv1a64(a + b); universal_hash validates the family’s parameters and reduces in the order that carries the 1/m1/m guarantee:

def fnv1a64(data: bytes, h: int = FNV_OFFSET) -> int:
h &= MASK64
for byte in bytes(data):
h ^= byte # xor first, then multiply: that order is the "1a"
h = (h * FNV_PRIME) & MASK64
return h
def universal_hash(x: int, a: int, b: int, p: int, m: int) -> int:
# ... ValueError unless 1 <= a < p, 0 <= b < p, and m >= 1
return ((a * x + b) % p) % m # mod the prime first, then into m buckets

The full solution is rng.py.

Start with cost. next_u32 is a constant number of big-int operations on at most 128-bit values; uniform is two draws; below needs fewer than two draws on average, since the rejected fraction is t/232<1/2t / 2^{32} < 1/2; shuffle is O(n)O(n) draws; fnv1a64 is O(bytes)O(\text{bytes}). Python makes all of this slow next to C, but training draws one number per weight or per mask element, which is fine at this scale, and the Rust engine ports the same code.

Why not numpy’s or Python’s generator? They are excellent generators, but their streams are not specified across languages, so a Python sampler and a Rust engine could never agree token for token, and a checkpoint could not store the generator in a portable form.

Why PCG32 rather than a raw LCG or xorshift? A raw LCG’s low bits have tiny periods, and PCG hides them behind the permutation at almost no cost. xorshift generators fail some statistical tests on their low bits and have no cheap stream selection. PCG32 has small state (two words), streams, and published reference outputs.

Why rejection rather than r % n or floating-point scaling? r % n is biased. floor(uniform() * n) is also biased for large nn and depends on floating-point rounding. Rejection is exact and portable. Lemire’s multiply-shift method is faster but would change the stream, and the spec fixes this one.

Why derive sub-streams from the seed? Deriving a child from the parent’s current state would tie dropout to the number of initialization draws, so changing the model size would change the dropout masks.

One tradeoff is accepted: every arithmetic step carries an explicit mask. It is noise to read, but it is exactly the reduction modulo 2642^{64} the mathematics requires, and dropping one mask is the bug that matches for a while and then diverges.

Most of the checks a correct solution must pass are published vectors, so a port in any language can be held to the same oracle. The table lists them with the downstream reason for each.

CaseKind of checkChecksWhy it matters downstream
Seed 0 by handunitthe hand trace: the seeded state and 0x47C28B93pins the seeding procedure
O’Neill’s demo linegoldenPCG32(42) gives O’Neill’s published first six outputsan oracle outside the books
1024 outputsgolden1024 outputs for seeds 0, 1, 2632^{63}every language’s stream
Bit-exact uniformsgoldenthe spec’s uniform_f64 vectors, also through uniformsthe sampler’s one draw per token
53 bits, below 1propertyu⋅253u \cdot 2^{53} integral, u<1u < 1, more than 32 bits usedinverse-CDF sampling
below and shuffle vectorsgoldenbelow(10) and shuffle([0..9]) vectorsthe training loop replays its data order on resume
No modulo biasstatisticaln=3⋅230n = 3 \cdot 2^{30}: the share below 2302^{30} is 1/3, not 1/2unbiased sampling of large ranges
Bad n for belowboundaryn=0n = 0, negative, or above 2322^{32} raise
Sub-stream vectorsgoldenchild_seed and the first outputs of each purposeinit, dropout, shuffle, sample stay independent
Sub-stream independencepropertya sub-stream depends on the seed onlyadding dropout does not change init
SplitMix64 outputsgoldenSplitMix64 seeded 0 and 1234567, published outputsthe mixer is the standard one
FNV-1a vectorsgoldenFNV-1a 64 reference vectorsthe KV block hash in the paged block pool and the Rust engine
FNV-1a chainingpropertyfnv(b, fnv(a)) == fnv(a + b) on random byteschained block hashes
Universal hash by handunitthe hand-trace universal hash
Universal collision ratepropertyexhaustive over all (a,b)(a, b) for p=101p = 101: collisions at most 1/m+1/p1/m + 1/phash tables in Algorithms
State round trippropertyset_state(state()) continues the stream; an even inc is rejectedcheckpoints store the generator (trainer_state.json)
Negative n for uniformsboundaryuniforms(-1) raises, uniforms(0) is empty

The pitfalls are mostly small departures from the spec that produce a plausible stream matching nothing else. Each is listed with its symptom and the line that prevents it.

PitfallSymptomHow the solution avoids it
permuting the new state instead of the old onea plausible stream that matches no other languagesaves old before the step and permutes it
an even increment (seq << 1 without | 1)short period; wrong from the first outputinc = ((seq << 1) | 1) & MASK64
no & MASK64 in Pythoncorrect until the state first wraps, then wrong forevermasks after every multiply and add
a uniform from one 32-bit drawonly 32 random bits, so the lower 21 bits of a double are always zerotwo draws, 27 + 26 bits, scaled by 2−532^{-53}
seeding in the wrong order (seed before the first step)streams for nearby seeds look alike, and no vector matchesstep, add the seed, step, as in pcg32_srandom_r
j = below(i) in the shuffle (Sattolo)only cyclic permutations; no element ever stays putj = self.below(i + 1)
r % n without rejectionsmall values favoured: 1/2 instead of 1/3 for n=3⋅230n = 3 \cdot 2^{30}rejects r < t with t = (2**32 - n) % n
deriving a sub-stream from the current statedropout masks change when the number of init draws changesseeds the child from self.seed, never from _state
a different mixer (Murmur’s >> 33)child seeds differ from every other portSplitMix64’s shifts 30, 27, 31 and its two constants
multiply before xor (FNV-1, not FNV-1a)every KV block hash differs from the Rust engine’sh ^= byte before h *= FNV_PRIME
reducing mod mm before mod ppnot universal; clustered buckets((a * x + b) % p) % m
accepting an even inc in set_statea corrupt checkpoint resumes a non-PCG sequenceset_state raises on an even inc

Warning: A missing mask in Python is the worst of these, because it raises nothing. The outputs match the vectors until the state first exceeds 2642^{64}, and then they are wrong forever. Test with at least a few hundred outputs, not just the first one.

This generator is the single source of randomness for nearly everything that follows. In this book, Random variables, expectation, variance, and normal draws by Box-Muller builds normal(rng, n) from two uniform() draws.

In AI Engineering, the foundations chapter leans on it throughout. The op library and gradcheck_all draws dropout masks and gradcheck_all’s random inputs from it. In Module system and basic layers, layers default to PCG32(0).substream(purpose), so initialization and dropout use separate streams and adding dropout does not change the initial weights. Training loop and the autograd bigram samples text with one uniform() per token. In Safetensors for every dtype, atomic checkpoints, and the token stream, TokenStream draws its window starts from a PCG32 and saves the generator’s state in its cursor, so a resumed run reads the same batches.

Later, Sampling and logit processors draws one uniform() per token on stream(seed, sample) (spec/sampling.md), and the Paged KV block pool chains FNV-1a block hashes. The Rust port in Candle model runner, Rust KV cache, sampler and PCG32 and the Go port in An open-loop load generator are held to the same published vectors.

Almost every model section from Bengio’s neural probabilistic language model through the Llama-family model draws from this generator, including word2vec, the LSTM, GRU, and stateful RNN language model, the attention-origins sections (encoder-decoder with teacher forcing, Bahdanau, Luong), the 2017 Transformer (multi-head attention, positional encodings, the encoder-decoder Transformer), the objectives chapter (GPT, BERT, ELECTRA, fine-tuning heads, the evaluation harness), and the modern block (gated MLPs, GQA, multi-head latent attention, mixture of experts).

The multimodal chapters use it too: conv weights in Conv layers and pooling on autograd, patch, position, and block weights in the Vision transformer encoder, the dual encoder in Contrastive image-text: CLIP and SigLIP, Image preprocessing to the specification, the Whisper encoder, the Whisper decoder and checkpoint weights, Whisper decoding, and bootstrap_ci in Multimodal eval metrics, which draws its resamples from PCG32(seed).

Outside that book, the Python Bloom screen in Exact dedup: paragraph hashes, a Bloom screen, and a sort-merge confirm in Data Engineering derives stable bit positions from FNV-1a and SplitMix64, Near-duplicate dedup: MinHash, LSH, union-find, and decontamination builds on this generator too, and Bias and safety evals in AI Engineering resamples paired prompts with PCG32 so its bootstrap intervals are reproducible.

Production systems take each piece further:

  • The PCG family (pcg64, numpy’s default PCG64) adds 128-bit state, the DXSM output function, and jump-ahead in O(log⁡n)O(\log n); see numpy’s numpy/random/_pcg64.pyx and O’Neill’s pcg-cpp.
  • JAX’s counter-based threefry keys replace SplitMix64 sub-streams: a key splits into independent keys without any shared state, ideal for parallel devices; see jax/_src/prng.py.
  • Lemire’s nearly divisionless method replaces rejection in below with one multiply and rarely a division per draw.
  • xxHash and wyhash replace FNV-1a with many bytes per step using SIMD and better avalanche, still non-cryptographic; see xxhash.h (XXH3).
  • vLLM’s prefix caching chains block hashes like ours, hashing each KV block with its parent hash and extra keys (LoRA id, multimodal inputs); see hash_block_tokens in vllm/v1/core/kv_cache_utils.py.
  • O’Neill, “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation” (2014).
  • Knuth, The Art of Computer Programming, vol. 2, ch. 3 (linear congruential generators, the spectral test).
  • Steele, Lea, and Flood, “Fast Splittable Pseudorandom Number Generators” (2014).
  • Carter and Wegman, “Universal Classes of Hash Functions” (1979).
  • Lemire, “Fast Random Integer Generation in an Interval” (2019).
  • Noll’s FNV page (isthe.com/chongo/tech/comp/fnv).
  • Congruence: a≡b(modn)a \equiv b \pmod n when aa and bb have the same remainder mod nn.
  • LCG: a generator that steps st+1=(Ast+c) mod 264s_{t+1} = (A s_t + c) \bmod 2^{64}.
  • PCG32: an LCG whose output is a permutation (xorshift, then a state-chosen rotation) of the old state.
  • Stream: one of 2632^{63} sequences selected by the odd increment inc.
  • Rejection sampling: discarding draws below 232 mod n2^{32} \bmod n so that r % n is unbiased.
  • Fisher-Yates shuffle: the uniform shuffle that swaps position ii with a uniform j∈[0,i]j \in [0, i].
  • Universal family: a random choice of hash function under which any two distinct keys collide with probability at most 1/m1/m.
  • FNV-1a: a byte hash that xors each byte in and then multiplies by a fixed prime mod 2642^{64}.
  • SplitMix64: the mixer used to derive child seeds that depend only on the seed and the purpose.
  • Python integers are unbounded, so PCG’s 64-bit state transition explicitly applies & (2**64 - 1) after each multiply and add.
  • PCG32 is a 64-bit LCG whose state is hidden behind a permutation chosen by its top bits; seeded the same way, Python, Rust, and Go produce the same stream bit for bit.
  • A uniform double with all 53 bits takes two 32-bit draws and only integer arithmetic, so it is bit-exact across languages and never equals 1.0.
  • r mod n is biased unless nn divides 2322^{32}; rejection removes the bias.
  • Sub-streams derived with SplitMix64 keep initialization, dropout, shuffling, and sampling independent; FNV-1a chains over buffers, which is how a KV block hash extends its parent’s.
  1. Why does next_u32 compute the output from old and not from the new state?

    Answer

    Because the spec says so: PCG-XSH-RR permutes the state before the step. A version that permutes the new state is an equally plausible generator that matches no published vector and no other port. (The design reason is speed in C: the multiply and the permutation of the old state can run in parallel.)

  2. What goes wrong if one & MASK64 is dropped in next_u32?

    Answer

    Until the state first exceeds 2642^{64} the outputs are identical, because the permutation reads only bits below 64 at first. After that the state is a growing big integer: old >> 59 is no longer the top 5 bits of a 64-bit word, so the rotation and then everything else is wrong, forever, while the code raises nothing.

  3. Why is ((1 << 32) - n) % n the right threshold, and why is a value r < t rejected rather than r >= 2**32 - t?

    Answer

    (232−n) mod n=232 mod n(2^{32} - n) \bmod n = 2^{32} \bmod n, computed without a number above 2322^{32}. Rejecting the tt smallest values leaves 232−t2^{32} - t values, a multiple of nn, so each residue has the same number of preimages. Rejecting the tt largest values would also be unbiased, but it is a different stream; the spec fixes the low end.

  4. Why does shuffle call below(i + 1) and not below(i)?

    Answer

    Fisher-Yates picks j∈[0,i]j \in [0, i], so that element ii may stay where it is. With j∈[0,i)j \in [0, i) (Sattolo) no element ever stays put and only the (n−1)!(n-1)! cyclic permutations are produced, not all n!n!.

  5. Why does substream use self.seed and not self._state?

    Answer

    The seed never changes, while the state moves with every draw. Using the seed makes PCG32(s).substream("dropout") the same generator however many numbers the parent produced, so adding dropout, or changing how many weights are initialized, leaves every other purpose’s stream unchanged.