View License

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

» Watch video

5.0 | 4 ratings Rate this file 10 Downloads (last 30 days) File Size: 6.06 KB File ID: #6614 Version:

Mersenne Twister



23 Dec 2004 (Updated )

Mersenne Twister uniform pseudo-random number generator.

| Watch this File

File Information

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.

MATLAB release MATLAB 7 (R14)
Other requirements To build object code for any platform, type "mex twister.cpp".
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
08 Sep 2016 Hu Wanting

06 Aug 2009 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?

Comment only
02 Nov 2006 Giuppy iuppie

it works for me.
Thanks so much

27 Jul 2006 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

Comment only
22 Mar 2005 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();

04 Feb 2005 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!

02 Jun 2009 1.1

Removed binaries.

12 Jun 2009 1.2

Cleaned up file header information.

18 Jun 2009 1.3

Add copyright.

06 Aug 2009 1.5


01 Sep 2016

Updated license

Contact us