Node.js Mersenne Twister random number generator module

jwatte's picture

I made a Node.js module. It's a packaging and slight update of the JavaScript implementation of the Mersenne Twister random number generator (which is a really high quality generator, as well as pretty fast).

I'm hoping you can soon get it with npm:

$ npm install mersenne
$

Usage is simple:

r = require('mersenne');
r.rand(N) <-- return a random number between 0 and N-1, inclusive

You can also do more advanced things, like creating multiple instances, return random floating-point numbers, etc.

Comments

I updated this module. If you

I updated this module. If you install it now (version 0.0.3) you will get a few improvements:
1) seed() and seed_array() are new exported functions.
2) By default, the seed is random based on time in milliseconds.
3) The default range for rand() if you pass no range is 32768, to match normal libc rand() (used to be, if you passed no range, you'd get NaN)
4) there is a test.js file for testing the implementation a bit

This should get you going with the new goodness:
npm install mersenne

This should get you going with the new goodness too

This should get you going with the new goodness too O(∩_∩)O~

i am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content I found in you post. Thanks for sharing. christian louboutin shoes http://www.cheapchristianlouboutinshoes2012.com

Are you able to save and

Are you able to save and restore state? So you can be sure your not using overlapping number streams.

jwatte's picture

The mersenne module exports a

The mersenne module exports a MersenneTwister19937 class that you can instantiate. However, this class does not publicly expose the state array for saving/restoring.
If you care about high-quality random numbers, you may want to seed through something like /dev/urandom when you create a new instance.

kW X-port

Sorry to post it here, but your download of kW X-port doesn't work!
It says:
Access denied
You are not authorized to access this page.
I can't PM you anywhere, so this was the only way for me to contact you.
Please fix the broken link!

jwatte's picture

You need to register on the

You need to register on the site to download the installer.

useful thanks.. also easy to add seed(s)

Hi,

thanks, useful to have a better quality RNG in node.

Perhaps you can add the following couple of lines to mersenne.js to expose the seed ?

exports.seed = function(s) {
gen.init_genrand(s);
};

I found the seed helpful in generating cookies and random uuids.

jwatte's picture

I see what you're saying. I'm

I see what you're saying. I'm using the actual MersenneTwister19937 class when I need any control beyond "give me a random number," but that would make an easy improvement.