# [Numpy-discussion] match RNG numbers with R?

Yaroslav Halchenko lists at onerussian.com
Sun Apr 6 17:43:10 EDT 2014

```Hi NumPy gurus,

We wanted to test some of our code by comparing to results of R
implementation which provides bootstrapped results.

R, Python std library, numpy all have Mersenne Twister RNG implementation.  But
all of them generate different numbers.  This issue was previously discussed in
https://github.com/numpy/numpy/issues/4530 :  In Python, and numpy generated
numbers are based on using 53 bits of two 32 bit random integers generated by
the algorithm (see below).    Upon my brief inspection, original 32bit numbers
are nohow available for access neither in NumPy nor in Python stdlib
implementation.

I wonder if I have missed something and there is an easy way (i.e. without
reimplementing core algorithm, or RPy'ing numbers from R) to generate random
numbers in Python to match the ones in R?

Excerpt from
http://nbviewer.ipython.org/url/www.onerussian.com/tmp/random_randomness.ipynb

# R
%R RNGkind("Mersenne-Twister"); set.seed(1); sample(0:9, 10, replace=T)

array([2, 3, 5, 9, 2, 8, 9, 6, 6, 0], dtype=int32)

# stock Python
random.seed(1); [random.randint(0, 10) for i in range(10)]

[1, 9, 8, 2, 5, 4, 7, 8, 1, 0]

# numpy
rng = nprandom.RandomState(1);  [rng.randint(0, 10) for i in range(10)]

[5, 8, 9, 5, 0, 0, 1, 7, 6, 9]

--
Yaroslav O. Halchenko, Ph.D.
http://neuro.debian.net http://www.pymvpa.org http://www.fail2ban.org
Senior Research Associate,     Psychological and Brain Sciences Dept.
Dartmouth College, 419 Moore Hall, Hinman Box 6207, Hanover, NH 03755
Phone: +1 (603) 646-9834                       Fax: +1 (603) 646-1419