Core · interface

SavableRandom

A generator whose position can be read and written.

Explained in The loop.

interface SavableRandom
import type { SavableRandom } from '@driftengine/core';

In depth

The closure above cannot be rewound, and a resimulation has to be. A rollback loop restores a past tick and steps forward again; anything the replayed ticks draw from a generator must draw the same numbers they drew the first time, or the replay produces a different world from the run it is replaying. mulberry32 keeps its position in a closed-over variable, which is the right shape for every other caller and unreachable for this one.

The whole position is one int32, which is why this is an interface over a number and not a buffer: mulberry32 mixes a counter and holds nothing else. A save() is a number a caller can put in a snapshot beside the rest of a tick's state, and comparing two of them is ===.

The sequence is mulberry32's, unchanged and asserted against it. That is the promise, not an implementation note: AGENTS.md makes the seeded sequence a contract about stored replays and ghosts, so a second generator that drifted from the first by an increment would invalidate every one of them while passing every test that only read this file.

Methods

next

next(): number

The next value in [0, 1).

save

save(): number

The position, as one integer.

restore

restore(state: number): void

Resume from a position save returned.

ParameterTypeDescription
statenumber