Skip to content

Instantly share code, notes, and snippets.

@dbrockman
Created December 6, 2015 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrockman/cb960b8d0900dfab2fb8 to your computer and use it in GitHub Desktop.
Save dbrockman/cb960b8d0900dfab2fb8 to your computer and use it in GitHub Desktop.
export default createFromSeed(randomSeed());
export function createFromSeed(seed) {
if (!Number.isInteger(seed) || seed <= 0) {
throw new TypeError('seed must be an int > 0');
}
let value = seed;
const next = () => {
const hi = 16807 * (value >> 16);
const lo = 16807 * (value & 0xFFFF) + ((hi & 0x7FFF) << 16) + (hi >> 15);
value = lo > 0x7fffffff ? lo - 0x7fffffff : lo;
};
return max => Math.floor(next() / 0x7fffffff * max);
}
export function randomSeed() {
return 1 + Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - 1));
}
@dbrockman
Copy link
Author

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