lychees's picture
Upload 569 files
87b3b3a
/**
* @namespace
* This code is an implementation of Alea algorithm; (C) 2010 Johannes Baagøe.
* Alea is licensed according to the http://en.wikipedia.org/wiki/MIT_License.
*/
ROT.RNG = {
/**
* @returns {number}
*/
getSeed: function() {
return this._seed;
},
/**
* @param {number} seed Seed the number generator
*/
setSeed: function(seed) {
seed = (seed < 1 ? 1/seed : seed);
this._seed = seed;
this._s0 = (seed >>> 0) * this._frac;
seed = (seed*69069 + 1) >>> 0;
this._s1 = seed * this._frac;
seed = (seed*69069 + 1) >>> 0;
this._s2 = seed * this._frac;
this._c = 1;
return this;
},
/**
* @returns {float} Pseudorandom value [0,1), uniformly distributed
*/
getUniform: function() {
var t = 2091639 * this._s0 + this._c * this._frac;
this._s0 = this._s1;
this._s1 = this._s2;
this._c = t | 0;
this._s2 = t - this._c;
return this._s2;
},
/**
* @param {float} [mean=0] Mean value
* @param {float} [stddev=1] Standard deviation. ~95% of the absolute values will be lower than 2*stddev.
* @returns {float} A normally distributed pseudorandom value
*/
getNormal: function(mean, stddev) {
do {
var u = 2*this.getUniform()-1;
var v = 2*this.getUniform()-1;
var r = u*u + v*v;
} while (r > 1 || r == 0);
var gauss = u * Math.sqrt(-2*Math.log(r)/r);
return (mean || 0) + gauss*(stddev || 1);
},
/**
* @returns {int} Pseudorandom value [1,100] inclusive, uniformly distributed
*/
getPercentage: function() {
return 1 + Math.floor(this.getUniform()*100);
},
/**
* @param {object} data key=whatever, value=weight (relative probability)
* @returns {string} whatever
*/
getWeightedValue: function(data) {
var avail = [];
var total = 0;
for (var id in data) {
total += data[id];
}
var random = Math.floor(this.getUniform()*total);
var part = 0;
for (var id in data) {
part += data[id];
if (random < part) { return id; }
}
return null;
},
/**
* Get RNG state. Useful for storing the state and re-setting it via setState.
* @returns {?} Internal state
*/
getState: function() {
return [this._s0, this._s1, this._s2, this._c];
},
/**
* Set a previously retrieved state.
* @param {?} state
*/
setState: function(state) {
this._s0 = state[0];
this._s1 = state[1];
this._s2 = state[2];
this._c = state[3];
return this;
},
_s0: 0,
_s1: 0,
_s2: 0,
_c: 0,
_frac: 2.3283064365386963e-10 /* 2^-32 */
}
ROT.RNG.setSeed(Date.now());