1

I searched and got the below example as a way of setting seed for loop from an answer here

## Load packages and prepare multicore process
library(forecast)
library(future.apply)
plan(multisession)
library(parallel)
library(foreach)
library(doParallel)
n_cores <- detectCores()
cl <- makeCluster(n_cores)
registerDoParallel(cores = detectCores())
set.seed(123, kind = "L'Ecuyer-CMRG")
a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
identical(a,b)

I got the result to be FALSE

I also tried the answer I got here which is not reproducible as the answer claim. I beginning to suspect if I am missing out something which I do not know.

I am on Windows, I need help as regards why I am not getting what others are getting with the same R code.

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • 2
    If the result wasn't `FALSE` something would be seriously wrong. The RNG produces a sequence of random numbers. `set.seed` starts the sequence at a specific "state" (ensuring you always get the same sequence) but it is still a sequence. – Roland Oct 07 '20 at 10:30
  • 1
    @Roland There’s an answer in the linked thread which claims this works. But yeah, this *shouldn’t* work. – Konrad Rudolph Oct 07 '20 at 10:31
  • Also relevant: https://stackoverflow.com/a/58631543/6574038 – jay.sf Oct 07 '20 at 10:31

1 Answers1

2

You just need to re-set the seed before the second rnorm() call:

library(forecast)
library(future.apply)
plan(multisession)
library(parallel)
library(foreach)
library(doParallel)
n_cores <- detectCores()
cl <- makeCluster(n_cores)
registerDoParallel(cores = detectCores())
set.seed(123, kind = "L'Ecuyer-CMRG")
a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
set.seed(123, kind = "L'Ecuyer-CMRG")
b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
identical(a,b)
# [1] TRUE
duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • @DanielJames OK; in that case, can you edit your question to include a [mcve]? Since I didn't know what code you used to set up your parallelism, I just used the approach in the question you linked to – duckmayr Oct 07 '20 at 10:48
  • @DanielJames See my edit; the approach of simply re-setting the seed still works with your posted code – duckmayr Oct 07 '20 at 10:56
  • I commend your effort and patience, I still get `FALSE` as an answer even with your edited example. – Daniel James Oct 07 '20 at 11:00
  • @DanielJames crazy... I guess it really is a Windows issue. I noticed that you weren't re-setting the seed in between `rnorm()` calls, which would cause it to be `FALSE` on **any** system, so I thought that might be what was causing your issue... sorry this didn't solve it for you (don't have a Windows system to test on) – duckmayr Oct 07 '20 at 11:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222654/discussion-between-daniel-james-and-duckmayr). – Daniel James Oct 07 '20 at 11:02
  • 2
    Different parallelization mechanisms are used on *nix and windows systems. If you want reproducible random numbers with parallelization, use `doRNG`. – Roland Oct 07 '20 at 11:13