This forum is read only and just serves as an archive. If you have any questions, please post them on

9 years ago by FelipeBudinich

Code can be found here

Impact JS Mersenne Twister Plugin

This plugins allows you to generate pseudorandom numbers using a Mersenne Twister.

This plugin is based on the MT19937 algorithm, and code by Ben Lesh


As a submodule, from the git command line:

git submodule add lib/plugins/mersenne

Then require this module:


var prng = new ig.MersenneTwister(seed);

seed: An Integer to be used as the initial seed for the pseudorandom number generator, if undefined will be used.

var prng = new ig.MersenneTwister(1);
var prng = new ig.MersenneTwister(42);
pnrg.getIntRange(-7, 5);


This method returns a pseudorandom float number between 0 and 1, including both.

.getIntRange(min, max)

This method returns a pseudorandom integer number between min and max, including both.

.getFloatRange(min, max)

This method returns a pseudorandom float number between min and max, including both.

9 years ago by Joncom

Is this preferable to Math.random()? I assume so, otherwise you probably wouldn't have gone to the effort. However, I'm curious to what degree is it better? And in what sort of use cases might such an improvement be noticeable?

9 years ago by FelipeBudinich

It is slower than Math.random(), and it uses more memory than Math.random().

If you need pure random (mgInunez is right, I should have said "feels more random" instead of "pure random"), it is better to use the Fisher–Yates shuffle, it has better distribution than Math.random(), and it is way faster and less memory intensive than Mersenne Twister.

But Mersenne Twister does have a purpose:

The main characteristic of this implementation of a pseudo random number generator ( PRNG ) is that numbers are pregenerated in an array using a seed.

If you use the same seed, you'll get the same sequence of numbers every time.

From the top of my head, that has 3 use cases:

Say you are playing Endless Sea (or another roguelike), and a player tries to cheat the random number generator saving just before a skill check (or closing the program), to reload the game until it's possible to pass a check with a 1% chance of happening.

Using a PRNG you can prevent that, because after every load, the PRNG will give you the same number sequence. That is, unless the player decides to take another action that causes a PRNG number to be provided before the check, and even then, it mitigates the effect of that kind of cheat, because the player does not know how many times he has to cause a check before it is possible to succeed on the check he wants.

Another use case, is to make the seed transparent to the player, in order to allow them to share it:

That is used in Dwarf Fortress to share pregenerated worlds, and in The Binding of Isaach Rebirth, allowing players to share a procedurally generated dungeon with other players. (Say you want to play the same map that Northernlion is playing on youtube).

And the third use case is to be use it on a play by post game ( a forum game for example).

This time you only get the original posting time as seed, and even if the user edits the post later, the player will get the same number

PS: If you use the same seeds I provide on Usage you'll get the same numbers I did.

9 years ago by mglnunez

Another thing is that browsers have Math.random() implemented differently.

Also the Fisher–Yates shuffle is not pure random and cannot be a replacement for Math.random(). It's used to shuffle an array around and uses a PRNG to do it. I use it to shuffle an array of integers, and use those integers to iterate through an array randomly without repeating any index.

9 years ago by FelipeBudinich

You are absolutely correct mgInunez, I should have said that it feels more random.

PS: I've used FYS on a raffle game developed to be used on an event: there were 3 kinds of prizes (A hat, a shirt and a bottle) to be handed to people at the event, in total we had:

2 hats
2 shirts
8 bottles

I wanted them to be spreaded out, so we used FYS to give away:

1 hat
1 shirt
4 bottles

Then we regenerated the array to give away the next 6 prizes. It was a huge success.

The .shuffle() method of my array utilities plugin is an implementation of FYS

In a sense it can replace Math.random() if you want a range of integers that you want to get in a non repeating random sequence, you only need to generate an array containing those integers.

I'm gonna do that later this week, our client asked for another raffle, this time using attendant's phone numbers :)

9 years ago by alexandre

Thanks for the plugin. Will come in handy for the random world generator I am working on.
Page 1 of 1
« first « previous next › last »