-
-
Save jakearchibald/5b9a5d194008871efc8b5b70b5f37b1d to your computer and use it in GitHub Desktop.
Forkable random number generator for visual effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function random(seed) { | |
let state = seed; | |
const next = () => { | |
state |= 0; | |
state = (state + 0x6d2b79f5) | 0; | |
var t = Math.imul(state ^ (state >>> 15), 1 | state); | |
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; | |
return ((t ^ (t >>> 14)) >>> 0) / 4294967296; | |
}; | |
return { | |
next, | |
nextBetween: (from, to) => next() * (to - from) + from, | |
fork: () => random(next() * 2 ** 32), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's cool
I'm curious about how do you write these code ? and the magic code
0x6d2b79f5
where comes from :D