jeudi 12 juillet 2018

R: doParallel (FORK), foreach and random number generation

When running a foreach loop using the doParallel package in R and FORK, each worker will start off with the same random seed thus leading to duplicate 'random' numbers.

set.seed(1)
cl <- makeCluster(2, type = "FORK")
registerDoParallel(cl)
foreach(1:4, .combine = 'c') %dopar% {rnorm(1, mean = 0, sd = 1)}
stopImplicitCluster()

[1] -0.6264538 -0.6264538  0.1836433  0.1836433

What is the best way to solve this?




Aucun commentaire:

Enregistrer un commentaire