After reading the help page on set.seed, I am unsure about how to solve the
following problem. I need to call function f a thousand times. The list of
values returned by f, should be as random as possible. f calls g twice:
f <- function(){g1 <- g(1); g2 <- g(2); c(g1; g2)}
The function g in turn calls sample and returns a number, but also depends on
its argument, so, starting from the same seed, g(1) returns a different number
than g(2). I need each call to g to start with the same random number seed, so
that the values returned by f would not be affected by redefining it this way:
f <- function(){g2 <- g(2); g1 <- g(1); c(g1; g2)}
To make the randomness requirement more precise:
f.list <- lapply(1:1000, f)
vec1 <- sapply(f.list, function(x){x[1]})
vec2 <- sapply(f.list, function(x){x[2]})
The values of vec1 should be as iid as possible and the values of vec2 should be
as iid as possible.
Any help with what I need to add to f would be greatly appreciated.
David
_____________________________
David Bickel http://davidbickel.com
Research Scientist
Pioneer Hi-Bred International
Bioinformatics & Exploratory Research
7250 NW 62nd Ave., PO Box 552
Johnston, Iowa 50131-0552
515-334-4739 Tel
515-334-6634 Fax
david.bickel at pioneer.com, bickel at prueba.info
This communication is for use by the intended recipient and ...{{dropped}}
On Mon, 2 Aug 2004 17:50:44 -0500, "Bickel, David" <DAVID.BICKEL at PIONEER.COM> wrote:>After reading the help page on set.seed, I am unsure about how to solve the following problem. I need to call function f a thousand times. The list of values returned by f, should be as random as possible. f calls g twice: > f <- function(){g1 <- g(1); g2 <- g(2); c(g1; g2)} >The function g in turn calls sample and returns a number, but also depends on its argument, so, starting from the same seed, g(1) returns a different number than g(2). I need each call to g to start with the same random number seed, so that the values returned by f would not be affected by redefining it this way: > f <- function(){g2 <- g(2); g1 <- g(1); c(g1; g2)}This is a problem that's like what people doing simulations on parallel computers worry about. You might want to look at the sprng package. (I haven't used it, so I'm not sure it does what you need). Duncan Murdoch
My guess is that http://cran.r-project.org/src/contrib/Descriptions/rlecuyer.html might be relevant, too. Cheers, Andy> From: Duncan Murdoch > > On Mon, 2 Aug 2004 17:50:44 -0500, "Bickel, David" > <DAVID.BICKEL at PIONEER.COM> wrote: > > >After reading the help page on set.seed, I am unsure about > how to solve the following problem. I need to call function f > a thousand times. The list of values returned by f, should be > as random as possible. f calls g twice: > > f <- function(){g1 <- g(1); g2 <- g(2); c(g1; g2)} > >The function g in turn calls sample and returns a number, > but also depends on its argument, so, starting from the same > seed, g(1) returns a different number than g(2). I need each > call to g to start with the same random number seed, so that > the values returned by f would not be affected by redefining > it this way: > > f <- function(){g2 <- g(2); g1 <- g(1); c(g1; g2)} > > This is a problem that's like what people doing simulations on > parallel computers worry about. You might want to look at the sprng > package. (I haven't used it, so I'm not sure it does what you need). > > Duncan Murdoch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >