Each time I run the following code, the numbers in the vector result_seq
remain the same, since I have used set.seed(11)
before generating the vector.
However, it seems that even though I use set.seed(11)
again before I generate the numbers in result_par
, the numbers change every time I run the code.
library(snowfall)
snowfall::sfInit(parallel = TRUE, cpus = 4)
testFun = function(i) {
result <- rnorm(1,10,3)
}
nsim <- 10
set.seed(11)
result_seq <- sapply(1:nsim, testFun)
print(mean(result_seq))
set.seed(11)
result_par <- sfLapply(1:nsim, testFun)
print(mean(as.numeric(result_par)))
Why is this happening? What can I do to ensure obtain the random numbers generated during the snowfall parallelization are reproducible?