File Exchange

image thumbnail

Mersenne Twister

version (6.06 KB) by

Mersenne Twister uniform pseudo-random number generator.



View License

NOTE: Beginning in V7.1 (R14SP3), MATLAB® includes built-in support for the Mersenne Twister. The mex file here is only needed for versions prior to that.
TWISTER produces pseudo-random numbers using the Mersenne Twister algorithm by Nishimura and Matsumoto, and is an alternative to the built-in function RAND in MATLAB. It creates double precision values in the closed interval [0, 1-2^(-53)], and can generate 2^19937 - 1 values before repeating itself.

This is a Mex file implementation derived from a copyrighted C program by Takuji Nishimura and Makoto Matsumoto. See, e.g.,

Reference: M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30.

Comments and Ratings (6)

Hu Wanting

James Tursa

James Tursa (view profile)

To Joe Bunda: The included lcc is a C compiler, not a C++ compiler. You need a C++ compiler to compile twister.cpp. I did a quick look at the code and it looks like it wouldn't take much to convert it to a C routine that lcc would accept.

To Peter Perkins: Do you plan on including a C version in a later update for people without a C++ compiler? If not, would you mind if I made a go at it?

Giuppy iuppie

it works for me.
Thanks so much

Joe Bunda

I am using Matlab R2006a in Windows XP x64. When I issue the "mex twister.cpp" command, I receive an error message

Error: Could not detect a compiler on local system which can compile the specified input file(s) at C:\Program files\MATLAB\R2006a/bin
/ line 540.

I did check the installation to verify that the LCC compiler is installed

Kent Conover

It worked the first time in R14. By my test twister is significantly faster, it requires only 60% of the runtime taken by the Matlab m-file rand();

Steven A.

Thanks for providing this. While the built-in random number generator is more then adequate, this is a great addition for a few reasons. 1. It shows the power of MEX! Very cool indeed. 2. Shows how the Mathworks is on the bleeding edge of technology!


Updated license




Add copyright.


Cleaned up file header information.


Removed binaries.

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video