obj %dorng% ex
foreach
.R
expression to evaluate.%dorng%
returns the result of the foreach loop.
See %dopar%
. The whole sequence of RNG
seeds is stored in the result object as an attribute. Use
attr(res, 'rng')
to retrieve it.
%dorng%
is a foreach operator that provides an
alternative operator %dopar%
, which enable
reproducible foreach loops to be performed.
library(doParallel)cl <- makeCluster(2)registerDoParallel(cl)# standard %dopar% loops are _not_ reproducibleset.seed(1234)s1 <- foreach(i=1:4) %dopar% { runif(1) }set.seed(1234)s2 <- foreach(i=1:4) %dopar% { runif(1) }identical(s1, s2)[1] FALSE# single %dorng% loops are reproducibler1 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }r2 <- foreach(i=1:4, .options.RNG=1234) %dorng% { runif(1) }identical(r1, r2)[1] TRUE# the sequence os RNG seed is stored as an attributeattr(r1, 'rng')[[1]] [1] 407 -305383241 69318028 -1161659107 -348804806 -1127582349 -343284712 [[2]] [1] 407 1222430165 -1386318120 1144759979 748500427 690571908 329037778 [[3]] [1] 407 215137210 -1421031558 1452323561 -2060097535 712279522 -278425444 [[4]] [1] 407 -1017432992 1583835527 1997665660 821136125 1282044776 1045008570# stop clusterstopCluster(cl)# More examples can be found in demo `doRNG`## Not run:# demo('doRNG')# ## End(Not run)