1

Are there off-the-shelf approaches in R and Python that will allow me to generate identical PRNG results?

Background: I am trying to migrate some code from R to Python. It contains an algorithm that uses sample without replacement.

I can validate the migrated code by either (simpler) show that it produces identical results on a small set of inputs or (more complex) show that the results converge given a larger set of inputs.

Let's say I wanted to take the simple route. Obviously, the following do not produce identical results, because there are different PRNGs and implementations of sampling functions under the hood.

# R
set.seed(1)
sample(0:9,3)
# returns vector 2 3 4

and:

# Python
import random
random.seed(1)
random.sample(range(10),3)
# returns list 2 1 4

I could just suck it up and write my own PRNG, which would be educational but would require more programmer time (on non-optimized code). If it came to that I would probably opt for the more complex validation.

Are there any alternatives to doing so using existing libraries? I would prefer not to introduce third-party dependencies (like Java API). Currently leaning toward rPython implementation but open to anything.

update: This closely related question does not have an accepted answer, and it does not directly address random selection, just generating a random uniform distribution.

Community
  • 1
  • 1
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
  • 4
    You could always call R code from within Python to get your random values (or python from within R). It looks like at least one person has developed a package to accomplish the task of syncing the rng between R and Python https://github.com/GjjvdBurg/SyncRNG – Dason Sep 28 '16 at 13:50
  • 1
    @Dason thanks -- I'll wrap that into an answer (need to return a sample, which looks like to choose n out of x I need to do `shuffle(x)[:n]`) unless you want to write it – C8H10N4O2 Sep 28 '16 at 14:02

0 Answers0