Git Product home page Git Product logo

rng-js's Introduction

JavaScript seedable random number generator

The generator below, seeded with "Example" will produce the same values as below for each of these random number distributions across all browsers.

var rng = new RNG('Example');
rng.random(40, 50);  // =>  42
rng.uniform();       // =>  0.7972798995050903
rng.normal();        // => -0.6698504543216376
rng.exponential();   // =>  1.0547367609131555
rng.poisson(4);      // =>  2
rng.gamma(4);        // =>  2.781724687386858

The underlying algorithm is RC4 and uniform number generation is about 10x slower than Math.random in V8. What you get in exchange for that is a seedable generator and additional random distributions (see example). You can still get speed and these additional distributions by using Math.random as the core uniform number generator.

var rng = new RNG(Math.random);

When no seed is provided, one is created randomly from available entropy sources. Seeds that are not strings are run through JSON.stringify() before being used.

Here's how you would replace Math.random with a seeded generator.

Math.random = RNG.prototype.uniform.bind(new RNG('my seed'));

Finally, for fun, a dice roller,

var dice = RNG.roller('4d6 + 10');
dice(); // => 17
dice(); // => 11

Node.js Usage

var RNG = require('rng-js');

This module can also be browserified thanks to browserify-shim.

rng-js's People

Contributors

arik-so avatar kirbysayshi avatar skeeto avatar

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.