在R中,有没有办法将单个长的并行循环分成几个较短的循环并仍保持它们的可重复性和相同性?
我尝试使用doRNG
和foreach
软件包执行此操作,但是当我使用几个短循环时,RNG种子与使用一个长循环时的RNG种子不同因此结果不同。
====
示例代码
library(doRNG)
library(foreach)
library(doParallel)
# one loop
set.seed(123)
x1 <- foreach(i = 1:4) %dorng% sample(100, 5)
# several loops
set.seed(123)
x2 <- foreach(i = 1:2) %dorng% sample(100, 5)
# ... possibly doing other computations and then resetting seed ...
x3 <- foreach(i = 1:2) %dorng% sample(100, 5)
all.equal(c(x1), c(x2, x3)) # component 3 and 4 differ