Created
August 9, 2019 07:03
-
-
Save s-melnikov/469cc44862c02a5cf43ce6efec57bcf5 to your computer and use it in GitHub Desktop.
Random
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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