[R] How to properly re-set a saved seed? I've got the answer, but no explanation
William Dunlap
wdunlap at tibco.com
Fri Jan 6 21:32:31 CET 2012
In S+ you can do
orig <- .Random.seed
x <- runif(10)
set.seed(orig)
identical(x, runif(10)) # return TRUE
because S+'s set.seed interprets a non-scalar
input as a copy of an old .Random.seed and assigns
it in the proper location. (It throws an error
if it is not a valid value for .Random.seed.)
In R you use set.seed to set a proxy seed (an integer
that maps into the real seed) and, I think,
.Random.seed <<- orig
to set the real seed.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sarah Goslee
> Sent: Friday, January 06, 2012 11:21 AM
> To: Paul Johnson
> Cc: R-help
> Subject: Re: [R] How to properly re-set a saved seed? I've got the answer, but no explanation
>
> Hi Paul,
>
> Did you try looking at s1, or ?.Random.seed
>
> s1 <- 444
> set.seed(s1)
>
> will give you the same result as
>
> set.seed(444)
>
> but .Random.seed does not contain the seed (444), but the state of
> the RNG, and is an integer vector.
>
> You can save that state using
>
> s1 <- .Random.seed
>
> and restore it using
>
> .Random.seed <- s1
>
> but that's not the same thing that set.seed() does, or the information the
> latter requires.
>
> So you have two options:
> 1. save the integer seed and use set.seed() to assign it.
> 2. save the RNG state from .Random.seed and restore it later.
>
> The latter, by the way, is entirely reasonable to do. What the help warns
> you against is altering part of .Random.seed
>
> Unless I've completely misinterpreted your question, always possible,
> you're mixing two distinct but related types of information.
>
> Sarah
>
> On Fri, Jan 6, 2012 at 2:05 PM, Paul Johnson <pauljohn32 at gmail.com> wrote:
> > Hello, happy new year.
> >
> > I've come back to a problem from last spring. I need to understand
> > what what is the technically correct method to save a seed and then
> > re-set it. Although this example arises in the context of MT19937,
> > I'm needing to do this with other generators as well, including
> > L'Ecuyer streams.
> >
> > The puzzle is this comment in ?Random: "'set.seed' is the recommended
> > way to specify seeds."
> >
> > What I did not understand before, and can't make sense of now, is that
> > set.seed does not "re-set" a saved seed. Here's my working example:
> >
> >
> > ## Paul Johnson
> > ## April 20, 2011
> >
> > ## If you've never dug into the R random number generator and its use of the
> > ## default MT19937, this might be informative. It will also help you
> > ## avoid a time-consuming mistake that I've made recently.
> >
> > ## I've been developing a simulation and I want to save and restore random
> > ## streams exactly. I got that idea from John Chambers Software for
> > ## Data Analysis and I studied his functions to see how he did it.
> > ## The problem, as we see below, is that set.seed() doesn't do what I expected,
> > ## and I feel lucky to have noticed it now, rather than later.
> >
> > ## I wish set.seed did work the way I want, though, and that's why I'm
> > ## writing here, to see if anybody else wishes the same.
> >
> > ## Here's a puzzle. Try this:
> > set.seed(444)
> > s1 <- .Random.seed
> > runif(1)
> > rnorm(1)
> >
> > set.seed(444)
> > runif(1)
> > rnorm(1)
> > ## Those matched. Good
> >
> > ## Re-insert the "saved seed" s1
> > set.seed(s1)
> > runif(1)
> > rnorm(1)
> >
> > ## Why don't the draws match? I "put back" the seed, didn't I?
> > ## Hm. Try again
> > set.seed(s1)
> > runif(1)
> > rnorm(1)
> >
> > ## Why did those match? But neither matches cases 1 and 2.
> >
> > ## I was baffled & discouraged.
> >
> > ## The help page for random numbers says:
> > ## 'set.seed' is the recommended way to specify seeds.
> >
> > ## But, it doesn't say something like
> >
> > # set.seed(s1)
> >
> > ## is supposed to work. Ah. My mistake.
> >
> > ############ Here's the fix and the puzzle #######
> > ## To re-set the generator to its position at s1, it is required
> > ## to do what the help page seems to say we should not.
> >
> > .Random.seed <- s1
> >
> > runif(1)
> > #########################################
>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> 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.
More information about the R-help
mailing list