2017-04-11 21:43:09 +00:00
|
|
|
QUnit.test("MersenneTwister test uniform distribution", function(assert) {
|
|
|
|
const N = Math.pow(2, 17), expected = N * 1.35;
|
2017-04-10 17:49:32 +00:00
|
|
|
let mt = new MersenneTwister();
|
2017-04-07 10:56:18 +00:00
|
|
|
mt.seed(new Date().getTime());
|
2017-04-11 21:43:09 +00:00
|
|
|
let data = new Uint32Array(N);
|
|
|
|
for (let i = 0; i < data.length; ++i) {
|
|
|
|
data[i] = mt.int32();
|
|
|
|
}
|
|
|
|
for (let sh = 0; sh <= 24; ++sh) {
|
|
|
|
let bins = new Uint32Array(256);
|
|
|
|
for (let b of data) {
|
|
|
|
++bins[(b >>> sh) & 0xFF];
|
2017-04-07 10:56:18 +00:00
|
|
|
}
|
2017-04-11 21:43:09 +00:00
|
|
|
let variance = bins.reduce(function(a, v){ return a + Math.pow(v - N / bins.length, 2); }, 0);
|
|
|
|
assert.ok(variance < expected, "Expecting variance to be under " + expected + ", got " + variance);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("MersenneTwister test float distribution", function(assert) {
|
|
|
|
const N = Math.pow(2, 17), expected = N * 1.3;
|
|
|
|
let mt = new MersenneTwister();
|
|
|
|
mt.seed(new Date().getTime());
|
|
|
|
let bins = new Uint32Array(512);
|
|
|
|
for (let i = 0; i < N; ++i) {
|
|
|
|
++bins[(mt.real2() * bins.length) >>> 0];
|
2017-04-07 10:56:18 +00:00
|
|
|
}
|
2017-04-11 21:43:09 +00:00
|
|
|
let variance = bins.reduce(function(a, v){ return a + Math.pow(v - N / bins.length, 2); }, 0);
|
|
|
|
assert.ok(variance < expected, "Expecting variance to be under " + expected + ", got " + variance);
|
2017-04-07 10:56:18 +00:00
|
|
|
});
|