Skip to content

Instantly share code, notes, and snippets.

@s-melnikov
Created August 9, 2019 07:03
Show Gist options
  • Save s-melnikov/469cc44862c02a5cf43ce6efec57bcf5 to your computer and use it in GitHub Desktop.
Save s-melnikov/469cc44862c02a5cf43ce6efec57bcf5 to your computer and use it in GitHub Desktop.
Random
let seed = Date.now();
function randomA() {
seed = (5 * seed + 3) % 16;
return seed / 16;
}
function randomB() {
seed = (seed * 7919 + 1) & 0xffff;
// upper range is 0xffff (65535) so bring it down to 0-1
return seed / 0xffff;
}
let ctr = 0;
function randomC(n = 7) {
ctr++;
let value = ((((seed >> 9) & 1) ^ ((seed >> 1) & 1)) << 15) | (seed >> 1);
seed = value;
value >>= 8; // high byte
value += ctr;
value %= n;
return value / n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment