Set seed Does Not work on my Windows as I Copied from Different Examples Using R

221 views Asked by At

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.

1

There are 1 answers

6
duckmayr On

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