问题描述
我搜索并获得了以下示例,该示例为从答案here中为loop
设置种子的方式
## 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)
我得到的结果是FALSE
我还尝试了获得here的答案,该答案不能作为答案声明重复使用。我开始怀疑我是否错过了我不知道的东西。
我在Windows上,我为什么需要使用相同的R
代码无法获得别人所得到的帮助。
解决方法
您只需要在第二次rnorm()
调用之前重新设置种子即可:
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