JavaScript · Randomness · Reproducibility
Randomness you can replay: seeds in mazes and Boids
Author: Grundy · Checked: November 16, 2026
A maze exposes a bug once, or a flock takes an interesting path. How can you return to that exact experiment? We will use the site’s existing maze and Boids models to separate the starting number, generator implementation and simulation settings. Dotted-underlined terms have definitions on hover, click or keyboard focus.
1. Save the experiment’s inputs
Math.random() produces pseudo-random values, but the function does not let the caller choose or reset its initial seed. Saving one output therefore does not let us restart the sequence. The site’s models instead use their own An algorithm that calculates a sequence from a changing internal state. Its output can look random while remaining reproducible for a fixed implementation and starting state. with an explicit The initial number used to start a pseudo-random generator. Recreating the generator with the same seed restarts its sequence; calling an existing generator advances it.. A reproducible run needs the same starting number, implementation, settings and order of calls. This applies to a model’s data: canvas size, theme and playback position describe how that data is displayed.
2. The maze generator changes state on every call
The maze generator createMazeRandom validates the seed and returns a function. Both it and its validation helper validateMazeSeed are shown below; this TypeScript example can run on its own. The returned function retains its The values a generator needs to calculate its next result. Here this is a number retained by the function returned from createMazeRandom. It changes after every call. in state; each call updates it and returns a value in [0, 1). This generator implementation has the version name mulberry32-v1. Math.imul performs 32-bit integer multiplication, >>> 0 converts the result to an unsigned 32-bit value, and dividing by 0x1_0000_0000 scales it by 2³². The bit operations mix the state; they are not instructions to choose a new seed. Accepted seeds are integers from 0 to 4294967295, including zero.
export function validateMazeSeed(seed: number): number {
if (typeof seed !== "number") throw new TypeError("Expected numeric uint32 seed");
if (!Number.isInteger(seed) || seed < 0 || seed > 0xffff_ffff) throw new RangeError("Expected uint32 seed");
return seed;
}
export function createMazeRandom(seed: number): () => number {
let state = validateMazeSeed(seed) >>> 0;
return () => {
state = (state + 0x6d2b79f5) >>> 0;
let value = Math.imul(state ^ (state >>> 15), state | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 0x1_0000_0000;
};
}3. Recreate the generator for an independent run
Two calls to createMazeRandom(93717) create independent functions with the same initial state. If we call both the same number of times, their results match. Reusing one function for two experiments instead continues the sequence: the second experiment starts at a later Which output a consumer reads: first, second, third and so on. One extra call moves all later consumers one position forward, even if the original seed stays unchanged.. Creating a generator inside every individual draw causes the opposite mistake: it repeatedly returns the first value. In the example below, first and second are independent; shared is deliberately shared. This is demonstration code using the site’s function.
const first = createMazeRandom(93717);
const second = createMazeRandom(93717);
const a = [first(), first(), first()];
const b = [second(), second(), second()];
// a and b contain the same numbers.
const shared = createMazeRandom(93717);
const c = [shared(), shared(), shared()];
const d = [shared(), shared(), shared()];
// c uses positions 1–3; d uses positions 4–6.4. Compare two independent runs
Each panel below calls the site’s real functions again: createMazeRandom for four sample numbers, generationSteps for a 7 × 5 maze, and createBoids followed by advanceBoids for a 16-bird flock. The maze shows the final Depth-first search. This maze generator follows unvisited neighbors and backtracks when it cannot continue; random choices determine which eligible neighbor it visits next. result; Boids starts with a circular obstacle and all three rules enabled. The slider selects one of 61 saved states, from tick 0 to tick 60. Build twice with the same seed, then change only the right seed. Equality is checked on full data, not rounded labels or images. Different seeds are not a general guarantee of different visible outcomes.
At the upper limit, seed + 1 wraps to 0. Each build recreates both runs. The slider retains its selected tick.
Numbers: match. Maze walls: match. Boids: match.
Left run: 93717
First four maze-generator values
0.886349 · 0.538047 · 0.907280 · 0.479469
DFS · 7 × 5
Boids · tick 0 · 16 birds
Right run: 93717
First four maze-generator values
0.886349 · 0.538047 · 0.907280 · 0.479469
DFS · 7 × 5
Boids · tick 0 · 16 birds
Numbers above are rounded to six decimal places for display. Equality uses complete values, maze wall masks and bird coordinates and velocities.
5. A seed does not choose a maze by itself
In the site’s maze-generation comparison, generationSteps creates a fresh generator for each method. DFS randomly chooses an eligible neighbor, Prim chooses an edge from its frontier, and Kruskal shuffles the edge list before processing it. A common seed gives the same starting sequence, but each algorithm uses it differently. The resulting mazes can therefore differ. Neighbor order matters too: DFS uses north/east/south/west order, while the edge list is built row by row with east before south. Changing the grid dimensions or these ordering rules can change the maze without changing the seed.
6. Boids uses a different generator
The flock-initialization function createBoids initializes a local variable from config.seed and uses a A generator that multiplies its state by a constant, adds another constant and keeps a bounded integer result. Boids uses Math.imul and >>> 0 to keep the update in 32-bit arithmetic.. It has different update rules from the maze generator, so the same numeric seed does not mean the same sequence across these two models. The generator assigns initial positions, directions and speeds. The code below shows the random-number part of createBoids. Here config holds validated flock settings and config.seed is their starting number. The Boids model belongs to the site: AlgiViz provides playback tools, not this simulation’s random-number policy.
let seed = config.seed;
const random = () => {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
return seed / 4294967296;
};7. Rejected positions consume numbers too
During initialization, a bird’s position is sampled again if it is too close to an obstacle. Each failed attempt consumes another pair of values before the code chooses an angle and speed. Changing the obstacle can therefore change how many values the first bird consumes, shifting the starting values of later birds. Adding a random() call just to print a debug value can shift them as well. To inspect a value already used by the model, store and log that value. In this block, random is the function above, config.count is the bird count and config.obstacles lists obstacles. boidsWorld is an 800 × 500 field; boidsPhysics.birdRadius is 3. The helper obstacleContact(x, y, o).distance returns the distance to the obstacle boundary, negative inside it. The position check therefore leaves an 11-unit margin from that boundary.
const birds = Array.from({ length: config.count }, (_, id) => {
let x: number, y: number;
do {
x = random() * boidsWorld.width;
y = random() * boidsWorld.height;
} while (config.obstacles.some(o => obstacleContact(x, y, o).distance < boidsPhysics.birdRadius + 8));
const angle = random() * 2 * Math.PI,
speed = 1.8 + random() * 0.8;
return Object.freeze({ id, x, y, vx: Math.cos(angle) * speed, vy: Math.sin(angle) * speed });
});8. Repeat model ticks, not elapsed wall time
After initialization, the current advanceBoids implementation does not request new random values. Each One discrete update of the model. Here each call to advanceBoids calculates the next flock state. A tick is separate from a browser screen refresh. calculates all birds from the same previous state and increments tick by one. In the excerpt, decision calculates steering forces and integrate updates position and velocity while handling speed limits, obstacles and world boundaries. In the same runtime, repeating the same starting configuration and number of updates reproduces the model state. At a different playback speed, ten seconds of viewing may select another state, so compare a tick index rather than a wall-clock duration. To resume from the middle, replay earlier ticks or save a sufficiently complete Saved values describing a particular model state. A seed can recreate the starting state; resuming halfway requires either recalculating earlier updates or restoring enough saved state.. A seed alone is not a saved flock halfway through its flight.
export function advanceBoids(state: BoidsState): BoidsState {
// Every decision reads the same immutable input, never partially updated birds.
const birds = state.birds.map(bird => integrate(state, bird, decision(state, bird).total).bird);
return Object.freeze({ config: state.config, tick: state.tick + 1, birds: Object.freeze(birds) });
}9. Keep settings and implementation versions
For a maze bug report, keep seed, width, height, the generation method and implementation versions. The example records mulberry32-v1 for the number sequence and grid-generators-v1 for the maze-generation rules. The object below illustrates a report; it is not a new player-link format. For Boids, keep count, obstacle, all rules switches and the chosen tick as well as the seed. There is no separate exported Boids model-version constant today, so keep the exact source used for the run when long-term replay matters. A later code change can alter the result even if the saved input remains valid.
// An example bug report, not a new site link format.
const reproduction = {
seed: 93717,
width: 7,
height: 5,
method: "dfs",
randomVersion: "mulberry32-v1",
generationVersion: "grid-generators-v1"
};10. Test repeatability and its limits
Check independently created runs with seed 0, a normal value and 4294967295; reject negative, fractional and out-of-range input. Compare complete wall arrays and bird states. Verify that an extra draw shifts the sequence and that rebuilding resets it. Fixed integer generator operations are easier to specify exactly than an entire simulation using Calculations using JavaScript Number values. Many real numbers are represented approximately; changing operation order or mathematical implementations can affect the result. and Math.sin or Math.cos: this article does not promise bit-for-bit Boids results across every runtime or future implementation. Reproducibility is an explicit agreement about inputs and execution.