[R] How to properly re-set a saved seed? I've got the answer, but no explanation
Sarah Goslee
sarah.goslee at gmail.com
Fri Jan 6 20:21:20 CET 2012
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
More information about the R-help
mailing list