Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating same random number sequence in Python, NumPy and R

Python, NumPy and R all use the same algorithm (Mersenne Twister) for generating random number sequences. Thus, theoretically speaking, setting the same seed should result in same random number sequences in all 3. This is not the case. I think the 3 implementations use different parameters causing this behavior.

R
>set.seed(1)
>runif(5)
[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819
Python
In [3]: random.seed(1)

In [4]: [random.random() for x in range(5)]
Out[4]: 
[0.13436424411240122,
 0.8474337369372327,
 0.763774618976614,
 0.2550690257394217,
 0.49543508709194095]

NumPy
In [23]: import numpy as np

In [24]: np.random.seed(1)
In [25]: np.random.rand(5)
Out[25]: 
array([  4.17022005e-01,   7.20324493e-01,   1.14374817e-04,
         3.02332573e-01,   1.46755891e-01])

Is there some way, where NumPy and Python implementation could produce the same random number sequence? Ofcourse as some comments and answers point out, one could use rpy. What I am specifically looking for is to fine tune the parameters in the respective calls in Python and NumPy to get the sequence.

Context: The concern comes from an EDX course offering in which R is used. In one of the forums, it was asked if Python could be used and the staff replied that some assignments would require setting specific seeds and submitting answers.

Related:

  1. Comparing Matlab and Numpy code that uses random number generation From this it seems that the underlying NumPy and Matlab implementation are similar.
  2. python vs octave random generator: This question does come fairly close to the intended answer. Some sort of wrapper around the default state generator is required.
like image 877
Nipun Batra Avatar asked Mar 06 '14 01:03

Nipun Batra


People also ask

Can you generate the same random numbers everytime?

random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function. Let's see how to set seed in Python pseudo-random number generator.

Is NumPy similar to R?

NumPy belongs to "Data Science Tools" category of the tech stack, while R can be primarily classified under "Languages". NumPy is an open source tool with 11.1K GitHub stars and 3.67K GitHub forks. Here's a link to NumPy's open source repository on GitHub.


2 Answers

use rpy2 to call r in python, here is a demo, the numpy array data is sharing memory with x in R:

import rpy2.robjects as robjects

data = robjects.r("""
set.seed(1)
x <- runif(5)
""")

print np.array(data)

data[1] = 1.0

print robjects.r["x"]
like image 110
HYRY Avatar answered Sep 30 '22 01:09

HYRY


I realize this is an old question, but I've stumbled upon the same problem recently, and created a solution which can be useful to others.

I've written a random number generator in C, and linked it to both R and Python. This way, the random numbers are guaranteed to be the same in both languages since they are generated using the same C code.

The program is called SyncRNG and can be found here: https://github.com/GjjvdBurg/SyncRNG.

like image 23
GjjvdBurg Avatar answered Sep 30 '22 00:09

GjjvdBurg