R码能否影响foreach输出中的随机数?
我使用R码能否影响foreach输出中的随机数?,r,random,foreach,doparallel,R,Random,Foreach,Doparallel,我使用foreach和doParallel运行了一个模拟,并与随机数进行了斗争(在代码中命名为random) 简而言之:我模拟了一个足球联赛,随机生成所有比赛的赢家和相应的结果。在dt_base中没有比赛,在dt_ex1和dt_ex2中已经知道4场比赛的结果。应模拟所有未知结果 在本文底部的联盟模拟代码中,我设置了1000个模拟,分为100个块(forloop用于向PostgreSQL发送数据,并在我使用的完整代码中减少RAM使用)我希望所有的随机数都是不同的(甚至不要坚持重复的结果)。 1。当
foreach
和doParallel
运行了一个模拟,并与随机数进行了斗争(在代码中命名为random
)
简而言之:我模拟了一个足球联赛,随机生成所有比赛的赢家和相应的结果。在dt_base
中没有比赛,在dt_ex1
和dt_ex2
中已经知道4场比赛的结果。应模拟所有未知结果
在本文底部的联盟模拟代码中,我设置了1000个模拟,分为100个块(forloop用于向PostgreSQL发送数据,并在我使用的完整代码中减少RAM使用)我希望所有的随机数都是不同的(甚至不要坚持重复的结果)。
1。当运行给定的代码时,应实现所有不同随机数的目标
> # ====== Distinct Random Numbers ======
> length(unique(out$random)) # expectation: 22000
[1] 22000
> length(unique(out$random[out$part == "base"])) # expectation: 10000
[1] 10000
> length(unique(out$random[out$part == "dt_ex1"])) # expectation: 6000
[1] 6000
> length(unique(out$random[out$part == "dt_ex2"])) # expectation: 6000
[1] 6000
2。现在,请取消注释分配最终分数的代码片段*[tmp_sim]=3
(应该是带有!!!
的第60、61、67、68行),然后再次运行
> # ====== Distinct Random Numbers ======
> length(unique(out$random)) # expectation: 22000
[1] 10360
> length(unique(out$random[out$part == "base"])) # expectation: 10000
[1] 10000
> length(unique(out$random[out$part == "dt_ex1"])) # expectation: 6000
[1] 180
> length(unique(out$random[out$part == "dt_ex2"])) # expectation: 6000
[1] 180
那就是当它变得一团糟,对我来说毫无意义的时候<当向这些数据帧中添加两个数字时,dt_ex1
和dt_ex2
内的code>randomiter总是相同的
你是否也有同样的感受?你知道怎么回事吗
我试过R版本3.5.3和3.6.3。还尝试了dorn
软件包。总是一样的问题
联盟模拟代码
R使用的随机生成器(包括by
set.seed
和runif
)是全局的,适用于整个应用程序
您的问题似乎是因为生成器的访问在并行进程之间共享,但在这些进程之间不同步(即,它不是“线程安全的”),因此每个进程都有自己的生成器状态视图(因此,由于这种不同步的访问,不同的进程可以绘制完全相同的随机数)。相反,您应该为每个并行进程(在本例中为每个模拟)提供其自己的随机生成器,该随机生成器不在进程之间共享,并且(或模拟)相应
多线程是您关心的可复制的“随机”数字之一
事实证明,根本问题更多地是由进程之间共享的数据帧引起的,而不是由R的全局RNG引起的。请看这个问题。谢谢您的回答!我知道您在说什么。不过,这是代码中的一点变化(不影响随机数生成器)这就不同了。我不需要结果是可复制的。我尝试了doRNG包,它控制线程之间的种子,但没有成功。我会再试一次。这似乎是克服这个问题的唯一方法,因为这些结果没有意义,真的。
# League Simulation
rm(list = ls())
set.seed(666)
cat("\014")
library(sqldf)
library(plyr)
library(dplyr)
# ====== User Functions ======
comb4 = function(x, ...) { #function for combining foreach output
Map(rbind, x, ...)
}
# ====== Data Preparation ======
dt_base = data.frame(id = 1:10,
part = rep("base",10),
random = NA)
dt_ex1 = data.frame(id = 1:10,
part = rep("dt_ex1",10),
HG = c(1,3,6,NA,NA,2,NA,NA,NA,NA), # Home Goals
AG = c(1,3,6,NA,NA,2,NA,NA,NA,NA), # Away Goals
random = NA)
dt_ex2 = data.frame(id = 1:10,
part = rep("dt_ex2",10),
HG = c(1,3,6,NA,NA,2,NA,NA,NA,NA), # Home Goals
AG = c(1,3,6,NA,NA,2,NA,NA,NA,NA), # Away Goals
random = NA)
# ====== Set Parallel Computing ======
library(foreach)
library(doParallel)
cl = makeCluster(3, outfile = "")
registerDoParallel(cl)
# ====== SIMULATION ======
nsim = 1000 # number of simulations
iterChunk = 100 # split nsim into this many chunks
out = data.frame() # prepare output DF
for(iter in 1:ceiling(nsim/iterChunk)){
strt = Sys.time()
out_iter =
foreach(i = 1:iterChunk, .combine = comb4, .multicombine = TRUE, .maxcombine = 100000, .inorder = FALSE, .verbose = FALSE,
.packages = c("plyr", "dplyr", "sqldf")) %dopar% {
## PART 1
# simulation number
id_sim = iterChunk * (iter - 1) + i
# First random numbers set
dt_base[,"random"] = runif(nrow(dt_base))
## PART 2
tmp_sim = is.na(dt_ex1$HG) # no results yet
dt_ex1$random[tmp_sim] = runif(sum(tmp_sim))
# dt_ex1$HG[tmp_sim] = 3 # !!!
# dt_ex1$AG[tmp_sim] = 3 # !!!
## PART 3
tmp_sim = is.na(dt_ex2$HG) # no results yet
dt_ex2$random[tmp_sim] = runif(sum(tmp_sim))
# dt_ex2$HG[tmp_sim] = 3 # !!!
# dt_ex2$AG[tmp_sim] = 3 # !!!
# ---- Save Results
zapasy = rbind.data.frame(dt_base[,c("id","part","random")],
dt_ex1[,c("id","part","random")]
,dt_ex2[,c("id","part","random")]
)
zapasy$id_sim = id_sim
zapasy$iter = iter
zapasy$i = i
out_i = list(zapasy = zapasy)
print(Sys.time())
return(out_i)
}#i;sim_forcycle
out = rbind.data.frame(out,subset(out_iter$zapasy, !is.na(random)))
fnsh = Sys.time()
cat(" [",iter,"] ",fnsh - strt, sep = "")
}#iter
# ====== Distinct Random Numbers ======
length(unique(out$random)) # expectation: 22000
length(unique(out$random[out$part == "base"])) # expectation: 10000
length(unique(out$random[out$part == "dt_ex1"])) # expectation: 6000
length(unique(out$random[out$part == "dt_ex2"])) # expectation: 6000
# ====== Stop Parallel Computing ======
stopCluster(cl)