2

Looking to match the Python equivalent seed to match R w/ set.seed(123). Is this possible to do in Python, perhaps with some shared C library? Tried the below with a vary large range, and stalled out. Perpaps there is a better way to do this -- and likely to get many mismatches only using a 3 element vector.

import numpy as np
# R set.seed(123); rnorm(mean = 0, sd = 1, n = 10)
Rdat = np.array([-0.56047565, -0.23017749, 1.55870831])

for x in range(0, 100000000):
    mu, sigma = 0, 0.1 # mean and standard deviation
    np.random.seed(x)
    pyDat = np.random.normal(mu, sigma, 3)
    if Rdat[0]==pyDat[0] and Rdat[1]==pyDat[1] and Rdat[2]==pyDat[2]:
        print(x)
eyeOfTheStorm
  • 351
  • 1
  • 5
  • 15
  • 1
    @DYZ Does look like a similar question. Both answers from the other question don't seem to show a way to find the equivalent seed match between languages. One solution limits functions which can be applied to the seed, and the other seems to tackle by creating a shared dataframe between the two languages. Do you think it's even possible to brute force, to simply find the 123 R equiv in Python -- perhaps with a shared underlying C function? – eyeOfTheStorm May 16 '17 at 01:10
  • 1
    @eyeOfTheStorm: I have a very strong hunch that you won't find a matching seed using your brute force method, I think the differences lie in the implementation details of the pRNG algorithms. If you really want to try brute forcing, make sure you are doing float comparisons with at least some tolerance, or you might miss a matching seed due to floating point precision issues. – Marius May 16 '17 at 01:17
  • 2
    What makes you think that the two languages are using compatible random number generators? Unless they have the same underlying implementation, there won't be an equivalent seed to find. – Prune May 16 '17 at 01:19
  • Can you live with a large file of random numbers that is read and used by both implementations? – Prune May 16 '17 at 01:20
  • 2
    I don't think it's possible. Your best bet (but not trivial ... ) would be to write a Python function that calls/wraps the `libRmath` standalone library ... – Ben Bolker May 16 '17 at 01:22

0 Answers0