0
library(parallel)
RNGkind("L'Ecuyer-CMRG")
set.seed(100)
mclapply(1:4, function(i) rnorm(1))

The above codes work well. But how can we reproduce the random number in sequential process mode?

if we replace the above mclapply with lapply, the results will be changed.

Mike Zhang
  • 21
  • 1
  • 1
    You're asking the pseudo-random number generator in a single core to mimic the behaviour of 4 independent psuedorandom number generators on different cores. I don't think that's possible. (Though I'm happy to be proved wrong.) What you may have to do is specify the starting seed for each core separately, and then reset the seed in your single core solution in a corresponding manner. Even that won't work if any of the PRNGs use (say) a time component as they iterate. ... – Limey Jun 23 '22 at 18:51
  • 1
    Perhaps the only fool-proof solution is to genereate all the pseudo-random numbers you need in advance and then pass them to your function. Which, in your toy example, destroys the whole problem, I accept... – Limey Jun 23 '22 at 18:52
  • If you're looking for a way to get 100% reproducible, random number regardless of whether you parallelize or not, and regardless of the number of parallel workers, then [`future_lapply(..., seed = TRUE)`](https://future.apply.futureverse.org/reference/future_lapply.html) of **future.apply** will do the trick. – HenrikB Jun 24 '22 at 02:53

0 Answers0