foreach, doParallel and random generation

此生再无相见时 提交于 2019-12-08 19:07:27

Your worries are correct; random number generation does not magically work in parallel and further steps need to be taken. When using the foreach framework, you can use the doRNG extension to make sure to get sound random numbers also when done in parallel.

Example:

library("doParallel")
cl <- makeCluster(2)
registerDoParallel(cl)

## Inject the doRNG framework to above registered for each backend
library("doRNG")
registerDoRNG()

foreach(i = 1:100) %dopar% rnorm(1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!