Socket
Socket
Sign inDemoInstall

alea

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    alea

Implementation of the Alea PRNG by Johannes Baagøe


Version published
Maintainers
1
Install size
4.51 kB
Created

Readme

Source

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. The Mersenne Twister algorithm is a popular algorithm to replace random implemenetations, and has even been ported to JavaScript. Unfortunately, it depends on bitwise operations which can be slow in JavaScript. 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 import and export methods. While you can initialize two Alea PRNGs with the same seed, you cannot sync up a new PRNG after an old PRNG has already started running. This is useful for games where a new player joins and their local PRNG should sync up with the remote one.

var prng1 = new Alea(200)

prng1()
prng1()

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

var prng2 = Alea.import(prng1.export())

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

Acknowledgements

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

FAQs

Last updated on 14 Dec 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc