Skip to content

Instantly share code, notes, and snippets.

@mutoo
Last active December 18, 2015 12:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutoo/5780122 to your computer and use it in GitHub Desktop.
Save mutoo/5780122 to your computer and use it in GitHub Desktop.
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