Skip to content

Instantly share code, notes, and snippets.

@signalwerk
Created May 23, 2019 13:25
Show Gist options
  • Save signalwerk/377fdd8e453a0986e294c7767758e19c to your computer and use it in GitHub Desktop.
Save signalwerk/377fdd8e453a0986e294c7767758e19c to your computer and use it in GitHub Desktop.
Pseudorandom number generator (PRNG)
// 1993 Park-Miller linear congruential generator (LCG)
// https://en.wikipedia.org/wiki/Lehmer_random_number_generator
// Source: https://gist.github.com/blixt/f17b47c62508be59987b
// usage:
// let rand = LCG(42);
// rand() → 0.000944073311984539
// rand() → 0.5713628428056836
// rand() → 0.2557850731536746
const LCG = s => {
return () => {
s = Math.imul(48271, s) | 0 % 2147483647;
return (s & 2147483647) / 2147483648;
};
};
export default LCG;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment