Download this file
| 1 | // A seeded random source (mulberry32): the same seed draws the same |
| 2 | // numbers, so a run that failed can be run again exactly. |
| 3 | export function seeded (seed) { |
| 4 | let state = Number(seed) >>> 0 || 1 |
| 5 | const rand = () => { |
| 6 | state = (state + 0x6d2b79f5) >>> 0 |
| 7 | let t = state |
| 8 | t = Math.imul(t ^ (t >>> 15), t | 1) |
| 9 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61) |
| 10 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296 |
| 11 | } |
| 12 | return { rand, pick: a => a[Math.floor(rand() * a.length)], chance: p => rand() < p } |
| 13 | } |
| 14 | |