- // This snippet compares two PRNG algorithms using the incremental seed test
- // "Small Fast Counter 32"
- // This is a good algorithm from the PractRand test suite: http://pracrand.sourceforge.net/RNG_engines.txt
- // It passes stringent randomness tests (PractRand, TestU01).
- function sfc32(a, b, c, d) {
- return function() {
- a >>>= 0; b >>>= 0; c >>>= 0; d >>>= 0;
- var t = (a + b) | 0;
- a = b ^ b >>> 9;
- b = c + (c << 3) | 0;
- c = (c << 21 | c >>> 11);
- d = d + 1 | 0;
- t = t + d | 0;
- c = c + t | 0;
- return (t >>> 0) / 4294967296;
- }
- }
- // MWC1616, as used in Chrome's Math.random() circa 2015
- // This is NOT a good algorithm: https://medium.com/@betable/tifu-by-using-math-random-f1c308c4fd9d#.7p3lk9bvk
- // It fails stringent randomness tests.
- function mwc(a, b) {
- return function() {
- a = 36969 * (a & 65535) + (a >>> 16);
- b = 18000 * (b & 65535) + (b >>> 16);
- var result = (a << 16) + (b & 65535) >>> 0;
- return result / 4294967296;
- }
- }
- // Which one passes the seed test?
- let count = 0; setInterval(() => {
- console.log(
- Math.round(sfc32(count++,count++,count++,count++)()*100),
- Math.round(mwc(count++,count++)()*100),
- );
- }, 500);