Seeded random numbers in JavaScript

Math.random() can't be seeded. For procedural levels you need one that can.

SNP 102 #512 Filed js gamedev

A roguelike that can replay a level from its seed needs the same "random" numbers every time. mulberry32 is tiny and good enough for games:

javascript
function mulberry32(seed) {
  return () => {
    seed = (seed + 0x6d2b79f5) | 0
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296
  }
}

const random = mulberry32(20261016)
random() // the same number every run

It returns numbers in [0, 1) like Math.random(), so your randomInt and shuffle helpers just take it as an argument. Show the seed to players and they can share levels by number.