Can piece of R code influence random numbers in foreach output?

后端 未结 1 357
借酒劲吻你
借酒劲吻你 2021-01-27 09:29

I run a simulation using foreach and doParallel and struggling with random numbers (named random in the code).

In a nutshell: I sim

相关标签:
1条回答
  • 2021-01-27 10:22

    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? .

    0 讨论(0)
提交回复
热议问题