Mersenne Twister (random number generator)

Monkey Forums/User Modules/Mersenne Twister (random number generator)

Nobuyuki(Posted 2014) [#1]
This is a cross-platform (native, not pure) implementation of the Mersenne Twister. Supported languages are JS, Java, C++, and C#. I have AS3 in there, but just couldn't get it to compile, so I welcome anyone who wishes to contrib a fix :)



What's Mersenne Twister?
It's a pseudorandom number generator (PRNG) by M.Matsumoto and T.Nishimura. It's well-known for its relatively large period size, even distribution and speed (though newer PRNGs have come out in the past few years that are arguably faster with fewer drawbacks). Nonetheless, the canonical implementation of mt19937 has been ported to a wide variety of platforms, which have been collected here and wrapped to provide a (mostly) consistent interface across them.

Why use it?
The PRNG that comes with Monkey may be (mostly) consistent across platforms when seeded the same way, but its randomness distribution is not that great and is probably comparable to the old VB6 Rnd() in that regard. You'll get "better" randomness with mt19937. This is useful for shuffling large lists in a way that's far less likely to have repeating patterns that the end-user may detect over time (example: Music playlists always playing the same set of songs in a row; card decks always seeming to have the same cards come after each other).

What are the disadvantages?
It's probably a tiny bit slower than Rnd(). Consistency across platforms is not guaranteed, and is limited to the implementation's significant digits (this may change in the future with better wrapping of the existing functions). Some platforms may give lower precision than others. It's not cryptographically secure (neither is Rnd()).

How do I use it in my project?
Download or clone from the below URL, pop the /mt/ folder into your project, and Import mt.mt.

https://github.com/nobuyukinyuu/MersenneTwister