Maze generation needs randomness, but a maze you cannot reproduce cannot be tested. Today you build your own seeded generator so a given seed always yields the same stream of numbers, in any language.
Build a seeded xorshift32 generator whose output stream is fixed by its seed.
Randomly generated mazes are only useful here if they are reproducible: the same seed must always build the same maze, or no spec could pin one. Rather than depend on a language’s built-in generator (each does something different), we build our own tiny one so its output is identical everywhere.
The algorithm is xorshift32, three lines of exclusive-or and bit shifts on a 32-bit state that produce a long, well-mixed sequence. The seed is the entire memory of the generator: two generators with the same seed march through the exact same numbers forever. The one rule is that the state can never be zero, since xorshift on zero yields zero and the stream dies, so we bump a zero seed to 1. Keep the arithmetic to 32 bits and the stream is bit-for-bit the same in Go, Python, or JavaScript. That reproducible stream is the foundation every maze in this chapter stands on.
type RNG struct{ state uint32 }func NewRNG(seed uint32) *RNG {if seed == 0 { seed = 1 } // xorshift must not start at 0return &RNG{state: seed}}func (r *RNG) Next() uint32 {x := r.state // keep to 32 bits (mask 0xFFFFFFFF if no uint32)x ^= x << 13x ^= x >> 17x ^= x << 5r.state = xreturn x}