Creating same random number sequence in Python, NumPy and R Creating same random number sequence in Python, NumPy and R arrays arrays

Creating same random number sequence in Python, NumPy and R


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 robjectsdata = robjects.r("""set.seed(1)x <- runif(5)""")print np.array(data)data[1] = 1.0print robjects.r["x"]


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.