[Numpy-discussion] match RNG numbers with R?

Sturla Molden sturla.molden at gmail.com
Mon Apr 7 11:46:50 EDT 2014


Yaroslav Halchenko <lists at onerussian.com> wrote:

> it is in src/main/RNG.c (ack is nice ;) )... from visual inspection looks "matching"

I see... It's a rather vanilla Mersenne Twister, and it just use 32 bits of
randomness. An signed int32 is multiplied by 2.3283064365386963e-10 to
scale it to [0,1). Then they also have a function called "fixup" that
prevents 0 or 1 from being returned. If the generated random value x <= 0.0
they return 0.5*2.328306437080797e-10, and if (1.0-x)<=0.0 they return
1.0-0.5*2.328306437080797e-10. 

http://svn.r-project.org/R/branches/R-3-1-branch/src/main/RNG.c

Also note that the algorithm used to set the seed is important. If you set
a seed equal 1 in NumPy, that might not mean the same in R. And then of
course there is the algorithm used to sample integers. 

Sturla




More information about the NumPy-Discussion mailing list