Openturn matches are replay-safe because every input is recorded. If your game logic readsDocumentation Index
Fetch the complete documentation index at: https://openturn.io/docs/llms.txt
Use this file to discover all available pages before exploring further.
Math.random() or Date.now() directly, the replay will diverge. The engine hands you a deterministic RNG and a fixed now on every transition. Use them.
The RNG API
@openturn/core exports createRng(seed, snapshot?) and a DeterministicRng interface:
createRng(seed) where seed comes from createLocalSession({ seed }) or the server’s per-room seed. It then passes a seeded RNG into every transition and move context.
Use it in a move
rng is in scope for run and every core resolver context. Any draw you make is recorded in the match’s RNG trace; the replay materializer reruns the same draws and arrives at the same state.
Dice helpers
For tabletop-style logic, use the dice sugar instead ofint(...) + 1:
dice(count, sides) consumes count; advantage/disadvantage consume two.
Why not Math.random
Math.random() produces different numbers on every call. A replay that re-dispatches the action log will diverge from the original match on the first random draw. The engine has no way to recover.
The same goes for Date.now(), performance.now(), crypto.randomUUID(), and any other non-deterministic side effect. If you need the time, read it from context.now, which is the match’s recorded time (fixed during replay).
Dice-like inputs as payload
Sometimes the randomness is an input to the game, not a decision inside it (a physical die roll, a coin flip outside the app). Model it as an event payload:examples/games/pig-dice does: the player sends the roll value, and the game validates it. This keeps the reducer pure and the randomness audit-friendly.
RNG snapshots
rng.getSnapshot() returns the current state. You usually do not need this; the engine tracks RNG state inside the snapshot for replay. It is available if you want to pass the RNG state across a boundary (pausing a long-running simulation, say).
Related
- Reference: core for the
createRngand turn helpers. - Concepts: reducers and queued events for determinism requirements on resolvers.