-
-
Save tripzilch/3047a519f1a1957f817ed13562735740 to your computer and use it in GitHub Desktop.
Tiny PRNG (xorshift128), seeded with a string, 175 chars
This file contains 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
// By Piter Pasma | |
// (using the xorshift128 PRNG algorithm by Marsaglia) | |
seed="Flurp"; | |
S=Uint32Array.of(9,7,5,3); | |
R=(a=1)=>a*(a=S[3],S[3]=S[2],S[2]=S[1],a^=a<<11,S[0]^=a^a>>>8^(S[1]=S[0])>>>19,S[0]/2**32); | |
[...seed+'ThxPiter'].map(c=>R(S[3]^=c.charCodeAt()*S[0])); | |
// R() is now a function that returns a value between 0 and 1 | |
// R(a) will return a value between 0 and a | |
console.log(1 + Math.floor(R(100))); // outputs integer number between 1 and 100 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment