Coder Social home page Coder Social logo

node-alea's Introduction

Alea

A simple copy-and-paste implementation of Johannes Baagøe's Alea PRNG

Mostly packaged so I can easily include it in my projeccts. Nothing more

JavaScript's Math.random() is fast, but has problems. First, it isn't seedable, second, its randomness leaves a bit to be desired. Johannes Baagøe has done some great work in trying to find a more modern PRNG algorithm that performs well on JavaScript, and Alea seems to be the one that has come out ahead (benchmarks).

Installation

npm install alea

Usage

var Alea = require('alea')

var prng = new Alea() // add an optional seed param

var nextRandnum = prng() // just call the return value of Alea

Additions

Also adds the ability to sync up two Alea PRNGs via the importState and exportState methods.

var prng1 = new Alea(200)

prng1()
prng1()

// after generating a few random numbers, we will initialize a new PRNG

var prng2 = Alea.importState(prng1.exportState())

// this should echo true, true, true
console.log(prng2() == prng1())
console.log(prng2() == prng1())
console.log(prng2() == prng1())

The theory behind this is that while a server is running a simulation (for example, a game) and clients connect to the server, each client will run its own simulation without having to depend 100% on the server for every update of the simulation state. By importing the current generator state from the server, a client can join in at any time and have an accurate simulation fully in sync with the server.

Acknowledgements

Everything in this module was made by Johannes Baagøe. I just wanted this in npm. Read more on his homepage.

node-alea's People

Contributors

coverslide avatar sashee 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.