for loop - Use set.seed() with foreach() in R -


i running simulation using loop in r, want switch on foreach loop since faster. use set.seed() in loop, , use again foreach can obtain identical results.

for example, suppose have

x <- c() (i in 1:10){     set.seed(i)     x[i] <- rnorm(1) } 

how can same thing using foreach? don't think works:

x <- foreach(i = 1:10, ...) %dopar% {set.seed(i) ... } 

this works:

library (foreach)  fn<-function(i) {   set.seed(i)   y <- rnorm(1)   return(y) }  x<-foreach(i=1:10) %do% fn(i) print(x) 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -