I run a simulation using foreach
and doParallel
and struggling with random numbers (named random
in the code).
In a nutshell: I sim
The random generator used by R (including by set.seed
and runif
) is global and applies to the whole application.
It appears that your problem is happening because the generator's access is shared between parallel processes, but is not synchronized between these processes (that is, it's not "thread safe"), so that each process has its own view of the generator's state (so that, as a result, different processes can draw exactly the same random numbers due to this unsynchronized access). Instead, you should give each parallel process (each simulation in this case) its own random generator that's not shared between processes, and seed each process (or simulation) accordingly.
Multithreading is one of the many issues to consider when reproducible "random" numbers are something you care about.
As it turns out, the underlying issue is caused more by data frames being shared among processes, rather than R's global RNG. See this question Multithread computation with R: how to get all different random numbers? .