Last active
December 18, 2015 12:08
-
-
Save mutoo/5780122 to your computer and use it in GitHub Desktop.
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
function Random(initialValue) { | |
var A = 48271; | |
var M = 2147483647; // 2^31-1 | |
var Q = (M / A) >> 0; | |
var R = M % A; | |
initialValue %= M; | |
if (initialValue < 0) | |
initialValue += M; | |
var state = initialValue; | |
if (state == 0) | |
state = 1; | |
return { | |
randomInt: function() { | |
var tmpState = A * (state % Q) - R * (state / Q >> 0); | |
if (tmpState >= 0) | |
state = tmpState; | |
else | |
state = tmpState + M; | |
return state; | |
}, | |
random0_1: function() { | |
return this.randomInt() / M; | |
} | |
} | |
} | |
r = new Random(+new Date); | |
r.randomInt(); | |
r.random0_1(); | |
r.random0_1()*100>>0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment