'Foreach: Replicating seed outside of foreach
I am trying to store a seed within each repetition of the foreach loop. If something is going wrong with one repetition, then I want to be able to easily call up the seed and assess the simulated data. An example below shows how I am storing the seed with the generated data and drawing a value from a standard normal distribution.
set.seed(1)
library(foreach)
library(doRNG)
cl <- makeCluster(no_cores_to_use)
registerDoParallel(cl)
out <- foreach(i=1:2) %dorng% {
store <- .Random.seed
value <- rnorm(1)
result <- list()
result$seed <- store
result$value <- value
return(result)
}
stopCluster(cl)
out[[1]]$value
[1] 0.7457097
However, when I go to set the seed to repetition 1 (i=1), the matching rnorm value does not match what the foreach loop has produced.
.Random.seed <- out[[1]]$seed
rnorm(1)
[1] -0.3262334
I have seen some answers suggest to set.seed(i) within the foreach loop. Can anyone advise how to save the seed within each foreach loop, as in my code above?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
