[Numpy-discussion] Adopt Mersenne Twister 64bit?

Siu Kwan Lam siu@continuum...
Sun Mar 10 13:12:27 CDT 2013

Hi all,

I am redirecting a discussion on github issue tracker here.  My original post (https://github.com/numpy/numpy/issues/3137):

"The current implementation of the RNG seems to be MT19937-32. Since 64-bit machines are common nowadays, I am suggesting adding or upgrading to MT19937-64.  Thoughts?"

Let me start by answering to njsmith's comments on the issue tracker:

> Would it be faster?

Although I have not benchmarked the 64-bit implementation, it is likely that it will be faster on a 64-bit machine since the number of iteration (controlled by NN and MM in the reference implementation http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c) is reduced by half.  In addition, each generation in the 64-bit implementation produces a 64-bit random int which can be used to generate double precision random number.  Unlike the 32-bit implementation which requires generating a pair of 32-bit random int.

But, on a 32-bit machine, a 64-bit instruction is translated into 4 32-bit instructions; thus, it is likely to be slower.  (1)

> Use less memory?

The amount of memory use will remain the same.  The size of the RNG state is the same.

> Provide higher quality randomness?

My naive answer is that 32-bit and 64-bit implementation have the same 2^19937-1 period.  Need to do some research and experiments.

> Would it change the output of this program:
>   import numpy
>   numpy.random.seed(0)
>   print numpy.random.random()
> ?

Unfortunately, yes.  The 64-bit implementation generates a different random number sequence with the same seed. (2)

My suggestion to overcome (1) and (2) is to allow the user to select between the two implementations (and possibly different algorithms in the future).  If user does not provide a choice, we use the MT19937-32 by default.

        numpy.random.set_state("MT19937_64", …)   # choose the 64-bit implementation


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130310/363432de/attachment.html 

More information about the NumPy-Discussion mailing list