fuzzyworld

basin / files / src / rng.ts

rng.ts

741 B · history · blame

1/** A deterministic pseudo-random source: call it for the next float in [0, 1). */
2export type Rng = () => number;
3
4/** mulberry32 — tiny, fast, good-enough PRNG seeded by a 32-bit integer. */
5export function mulberry32(seed: number): Rng {
6 let a = seed >>> 0;
7 return function () {
8 a = (a + 0x6d2b79f5) | 0;
9 let t = Math.imul(a ^ (a >>> 15), 1 | a);
10 t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
11 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12 };
13}
14
15/** FNV-1a hash so a human-typed name can also seed a board. */
16export function hashStringToSeed(s: string): number {
17 let h = 2166136261 >>> 0;
18 for (let i = 0; i < s.length; i++) {
19 h ^= s.charCodeAt(i);
20 h = Math.imul(h, 16777619);
21 }
22 return h >>> 0;
23}