Skip to content

Instantly share code, notes, and snippets.

@jakearchibald

jakearchibald/random.js Secret

Last active Jan 6, 2021
Embed
What would you like to do?
Forkable random number generator for visual effects
export default function randomGenerator(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: () => randomGenerator(next() * 2 ** 32),
};
}
@Cu2y

This comment has been minimized.

Copy link

@Cu2y Cu2y commented Dec 16, 2020

export default function randomGenerator(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: () => randomGenerator(next() * 2 ** 32),
};
}

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