Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active May 10, 2022 10:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jakearchibald/5b9a5d194008871efc8b5b70b5f37b1d to your computer and use it in GitHub Desktop.
Save jakearchibald/5b9a5d194008871efc8b5b70b5f37b1d to your computer and use it in GitHub Desktop.
Forkable random number generator for visual effects
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),
};
}
@universeroc
Copy link

it's cool
I'm curious about how do you write these code ? and the magic code 0x6d2b79f5 where comes from :D

@jakearchibald
Copy link
Author

I have no idea, I just took it from https://github.com/bryc/code/blob/master/jshash/PRNGs.md 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment