[Numpy-discussion] match RNG numbers with R?

Sturla Molden sturla.molden at gmail.com
Sun Apr 6 18:38:15 EDT 2014

Yaroslav Halchenko <lists at onerussian.com> wrote:

> 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.

NumPy uses the Randomkit library. The source is here:


It very easy to modify to produce whatever result you want. You can build
it separately from NumPy.

RandomState.bytes and np.random.bytes gives you the original random bytes
in little endian order. It basically calles rk_fill in Randomkit and then
unpacks the 32 bit random integer into four bytes. Just view it as
dtype='<u4' to see the original 32-bit unsigned integers:

a = np.random.bytes(4*n).view(dtype='<u4')


More information about the NumPy-Discussion mailing list