Giter Site home page Giter Site logo

Comments (1)

GeorgesOatesLarsen avatar GeorgesOatesLarsen commented on September 3, 2024

Worth note: I did not take the time to properly understand this RNG before writing the above. So, it is possible that there are other state variables that would need to be included that I missed.

That being stated, the following test code would suggest strongly against that I have succeeded:

import MersenneTwister from "mersenne-twister";

let gen = new MersenneTwister();

//Warm up the twister
for(let i = 0; i < 1000; i++) {
    gen.random_int();
}

//Store the state of the twister
let ostate = gen.mt.slice();
let omti = gen.mti;

//Generate and store a large number of sequential samples of the twister
let ntest = 10000;
let firstrun = [];
for(let i = 0; i < ntest; i++) {
    firstrun.push(gen.random_int());
}

//Revert to the stored state
gen.mt = ostate.slice();
gen.mti = omti;

//Generate another large set of similar length of numbers.
//Compare each one to the original set.
//Each should be identical if the state was fully captured.
//Count the number of failures.
let failures = 0;
let passes = 0;
for(let i = 0; i < ntest; i++) {
    let a = gen.random_int();
    let b = firstrun[i];
    console.log(a, b);
    if (a !== b) {
        failures += 1;
    } else {
        passes += 1;
    }
}
console.log("\n\n\n" + failures + " failures, " + passes + " passes.");

And, some sample output:

2995799045 2995799045
2196720773 2196720773
2243876668 2243876668
2352771636 2352771636
1379316772 1379316772
1981010483 1981010483
3153207042 3153207042
3063185403 3063185403
2111342942 2111342942
1070999471 1070999471
2842412602 2842412602
319743464 319743464
586335663 586335663
2597304042 2597304042
2894075722 2894075722
3390066269 3390066269
2753316963 2753316963



0 failures, 10000 passes.

Finally, since I used es6 import syntax, if you want to run this without thinking too hard about it, make sure the extension is .mjs, and use:
node --experimental-modules mersennetest.mjs

from mersenne-twister.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.