mutoo
6/14/2013 - 7:35 AM

pseudorandom.js

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;