@pocketrts/determinism

Deterministic simulation

The building blocks that keep a lockstep simulation bit-identical on every device.

The same math on every engine

Math.sin and friends may differ in the last bit between Chrome, Safari and Firefox. dmath rebuilds them from operations IEEE 754 fixes exactly, so its fingerprint over fixed samples is the same everywhere.

This browser's dmath fingerprint:

How often this browser's Math disagrees with dmath in the last bit (10 000 random inputs each):

FunctionResults that differLargest difference

Replays give the same checksums

A swarm of 300 seeded particles steering with dmath and RNG. StateHasher hashes the whole state every 30 steps; run it again and every checksum matches. Let one particle take its jitter from Math.random once instead: that particle moves differently, and since run B also skips one draw of its seeded RNG, every later draw shifts and the whole swarm drifts apart.

Run A is drawn in blue, run B in red on top: while they agree, only red shows.

StepRun ARun BMatch

Seeded noise

fbm (fractal value noise over hash2) uses integer hashing and exactly rounded + − ×: the same seed draws the same terrain anywhere.

Commands as bytes

ByteWriter packs integers as varints and coordinates as float32. Peers execute what the bytes say, so the sender runs the decoded copy too: note how the coordinate is rounded.

Encoded bytes
Decoded command

Keep nondeterminism out of the source

findNondeterminism flags engine-dependent math, ** powers, engine randomness and clocks. Edit the code: