Dear All, I have three classes of questions about generating random numbers with different packages (windows xp 32bit R). . 1. Suppose I would like to use package *foreach*, can I use current Sys.time as a seed? Although I can get the time up to1e-6 second precesion, the code below dose not work well on a local machine with two cores. ################# library(foreach) library(snow) library(doSNOW) testfun=function(){ options(digits.secs=6) set.seed(slave.seed <- substring(Sys.time(), 21, 26)) a <- rnorm(5,0,1) return(a) } maxkk <- 5 cl <- makeCluster(5, type = "SOCK") registerDoSNOW(cl) results=foreach (i = 1:maxkk, .combine = 'rbind') %dopar% testfun() results stopCluster(cl) ################# 2. Suppose I want to use *snow*, according to some reference, I need to use *rsprng* as a generator. Does this generator relay on seed as well? But it seems to me that if I do not use rsprng, it is also fine. ## code without rsprng ## testfun <- function(){ a <- rnorm(5,0,1) return(a) } library(snow) cl <- makeCluster(5,type="SOCK") clusterCall(cl, testfun) stopCluster(cl) ## code with rsprng ## testfun <- function(){ a <- rnorm(5,0,1) return(a) } library(snow) library(rsprng) cl <- makeCluster(5,type="SOCK") clusterSetupRNG(cl) clusterCall(cl, testfun) stopCluster(cl) 3. Suppose I would like to use the package *parallel, *do I still need * rsprng? *Should I specify any seed in this case? testfun <- function(){ a <- rnorm(5,0,1) return(a) } library(parallel) cl <- makeCluster(5,type="SOCK") clusterCall(cl, testfun) stopCluster(cl) My questions can also be viewed as these aspects: how do these generators work with/without seed, what is the relation between these packages, what is the best option of seed if needed. Thank you very much for your help. Best wishes, Jie [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Aug-08 02:21 UTC
[R] random number generator with SNOW/ Parallel/ foreach
I believe this is discussed in the vignette (extended documentation) of the parallel package, in particular section 6: type vignette("parallel") to see. I've also attached a copy. Michael On Tue, Aug 7, 2012 at 7:57 PM, Jie <jimmycloud at gmail.com> wrote:> Dear All, > > I have three classes of questions about generating random numbers with > different packages (windows xp 32bit R). > . > 1. Suppose I would like to use package *foreach*, can I use current > Sys.time as a seed? > Although I can get the time up to1e-6 second precesion, the code below dose > not work well on a local machine with two cores. ################# > library(foreach) > library(snow) > library(doSNOW) > testfun=function(){ > options(digits.secs=6) > set.seed(slave.seed <- substring(Sys.time(), 21, 26)) > a <- rnorm(5,0,1) > return(a) > } > maxkk <- 5 > cl <- makeCluster(5, type = "SOCK") > registerDoSNOW(cl) > results=foreach (i = 1:maxkk, .combine = 'rbind') %dopar% testfun() > results > stopCluster(cl) > ################# > > 2. Suppose I want to use *snow*, according to some reference, I need to use > *rsprng* as a generator. Does this generator relay on seed as well? But it > seems to me that if I do not use rsprng, it is also fine. > ## code without rsprng ## > testfun <- function(){ > a <- rnorm(5,0,1) > return(a) > } > library(snow) > cl <- makeCluster(5,type="SOCK") > clusterCall(cl, testfun) > stopCluster(cl) > > ## code with rsprng ## > testfun <- function(){ > a <- rnorm(5,0,1) > return(a) > } > library(snow) > library(rsprng) > cl <- makeCluster(5,type="SOCK") > clusterSetupRNG(cl) > clusterCall(cl, testfun) > stopCluster(cl) > > 3. Suppose I would like to use the package *parallel, *do I still need * > rsprng? *Should I specify any seed in this case? > > testfun <- function(){ > a <- rnorm(5,0,1) > return(a) > } > library(parallel) > cl <- makeCluster(5,type="SOCK") > clusterCall(cl, testfun) > stopCluster(cl) > > My questions can also be viewed as these aspects: how do these generators > work with/without seed, what is the relation between these packages, what > is the best option of seed if needed. > Thank you very much for your help. > > Best wishes, > Jie > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-------------- next part -------------- A non-text attachment was scrubbed... Name: parallel.pdf Type: application/pdf Size: 292168 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120807/5ffe785b/attachment.pdf>
Possibly Parallel Threads
- problem with parLapply from snow
- clusterCall with replicate function
- Parallel computing with the snow package: external file I/O possible?
- Problem with clusterCall, "Error in checkForRemoteErrors(lapply(cl, recvResult)) : "
- Parallel computing with the snow package: external file I/O possible?