The parallel package comes with special support for the L'Ecuyer-CMRG random number generator, which was introduced simultaneously with parallel . You can read the documentation for this support using:
library(parallel) ?mc.reset.stream
To use it, you first need to enable "L'Ecuyer-CMRG":
RNGkind("L'Ecuyer-CMRG")
After this code, for example:
set.seed(1) mclapply(mylist, foo, mc.cores=3) mclapply(mylist, foo, mc.cores=3)
will be reproducible, but two calls to mclapply will return the same results. This is due to the fact that the state of the random number generator in the main process does not change, causing mclapply .
I used the following function to skip random number streams used by mclapply workers:
skip.streams <- function(n) { x <- .Random.seed for (i in seq_len(n)) x <- nextRNGStream(x) assign('.Random.seed', x, pos=.GlobalEnv) }
You can use this function to get the behavior that I think you need:
set.seed(1) mclapply(mylist, foo, mc.cores=3) skip.streams(3) mclapply(mylist, foo, mc.cores=3) skip.streams(3)
source share