3
votes

Suppose I fork an R process into 10 workers to run some parallel simulations. What would be a reliable method to make sure that each worker uses different random numbers? I been using this:

myseed <- (unclass(Sys.time())*1e9 * Sys.getpid()) %% 1e9;
set.seed(myseed);

However to my surprise I was getting problems due to RNG clashes between workers. What could have gone wrong here? Is there a better source of randomness that I can use to seed the RNG?

1
Try the doRNG package, which is built expressly for this purpose.Simon O'Hanlon
There is also the old granddaddy of parallel RNGs in package rsprng (which is also in Debian / Ubuntu) relying on the SPRNG library (which also is packaged).Dirk Eddelbuettel

1 Answers

5
votes

See vignette("parallel") section 6 for details of how this can be done using functions from that package, which seem independent of using parallel's functions for parallel runs.

The example given there is:

RNGkind("L'Ecuyer-CMRG")
set.seed(<something>)
## start M workers
s <- .Random.seed
for (i in 1:M) {
  s <- nextRNGStream(s)
  # send s to worker i as .Random.seed
}

That vignette also mentions the rstream and rlecuyer packages, in addition to the doRNG package, that @Simon O'Hanlon mentions in his comment, and the rsprng package mentioned by @Dirk.