node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »


An efficient Mersenne Twister implementation using the Java Random interface.


This code was originally in C, and then ported to Javascript. Havvy cleaned it up, removed the functions that weren't useful to me, and added some more functions.

The original authors are Takuji Nishimura and Makoto Matsumoto


The library itself is a constructor that returns a '''Random''' object. It is not necessary to use ''new'' before the constructor.

var Random = require('random');
var random = new Random(42);

The Random object implements next(), nextInt(), and nextBoolean() as per Java's API. nextElement and shuffle are some random functions that are often done to arrays. Each element in the range has an equal chance of being selected.

'''next()''' - Returns a value between [0..1) in increments of 2^-32. '''nextInt(length = 4294967295) ''' - Returns an int between [0..length). '''nextBoolean()''' - Returns either true or false.

'''nextElement(list)''' - Returns an element from the list. '''nextElements(list, count)''' - Returns a new array with ''count'' random elements from ''list''.

'''shuffle(array)''' - Returns a new array with the contents of the passed in array shuffled. '''shuffleInPlace(list)''' - As the name says, shuffles a list in place, mutating the list.