Giter Site home page Giter Site logo

mersenne-twister's Introduction

Pseudorandom number generator Build Status

Mersenne Twister pseudorandom number generator.

Origin source (generator interface was changed)

Algorithm - http://en.wikipedia.org/wiki/Mersenne_twister

Installation

$ npm install mersenne-twister

Usage

var MersenneTwister = require('mersenne-twister');
var generator = new MersenneTwister();

// Generates a random number on [0,1) real interval (same interval as Math.random)
generator.random();

// [0, 4294967295]
generator.random_int();

// [0,1]
generator.random_incl();

// (0,1)
generator.random_excl();

// [0,1) with 53-bit resolution
generator.random_long();

// [0, 2147483647]
generator.random_int31();

Seeding

If you want to use a specific seed in order to get a repeatable random sequence, pass an integer into the constructor:

var generator = new MersenneTwister(123);

and that will always produce the same random sequence.

Also you can do it on existing generator instance:

generator.init_seed(123);

License

See source

mersenne-twister's People

Contributors

boo1ean avatar charleslparker avatar wtgtybhertgeghgtwtg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mersenne-twister's Issues

Typescript

Hi @boo1ean,
I would like to know if you are interesting in refactor this package to Typescript, I already refactored it in my side. Let me know, if yes, I'll open a PR with the proper changes.

Create interface for full state management

Variables mt and mti appear to fully determine the state of the generator.

I suggest implementing functions get_state and set_state that respectively return and receive a state object containing the keys mt and mt.

For Example:

MersenneTwister.prototype.get_state = function() {
    return {
        mti:this.mti,
        mt:this.mt.slice(),
    }
}

MersenneTwister.prototype.set_state = function(newstate) {
    this.mti = newstate.mti;
    this.mt = newstate.mt.slice();
}
MersenneTwister.prototype.set_state_unsafe = function(newstate) {
    this.mti = newstate.mti;
    this.mt = newstate.mt;
}

This would be useful in situations where it is necessary to revert to an arbitrary midpoint through a random progression. Currently, it is possible just by manually controlling mti and mt, but ideally a user would not need to understand the inner-workings of the mersenne twister in order to manage its state.

Run in nodejs OR browser

Changing the following would allow the same file to be loaded in either nodejs or the browser:

From:

/* These real versions are due to Isaku Wada, 2002/01/09 added */

module.exports = MersenneTwister;

To:

/* These real versions are due to Isaku Wada, 2002/01/09 added */

//Note process should exist only in nodejs, while window exists only in the browser
if(typeof process !== 'undefined' && typeof window === 'undefined')
{
	module.exports = MersenneTwister;
}

If it is not too inconvenient, please apply this change to the repo and update the npm version/package.

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.