Re: [R] Restoring .Random.seed

From: Talbot Katz <topkatz_at_msn.com>
Date: Thu, 31 May 2007 17:15:19 -0400


Thanks! The "get / assign" combination does just what I want, and the warning about the pre-existence of .Random.seed was very helpful. As for set.seed, I have used it to create a replicable state (in fact, I have an option to use it in the function I was writing that prompted my query), but I didn't see any indication that it could be used to restore a state for which you don't necessarily know the seed.

I got a couple of good offlist responses, too. One person told me about the "<<-" assignment operator (with an admonishment to use it judiciously). Another responder mentioned the setRNG package, which has a specific methodology for saving the random number generator state.

>From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
>To: Talbot Katz <topkatz_at_msn.com>
>CC: r-help_at_stat.math.ethz.ch
>Subject: Re: [R] Restoring .Random.seed
>Date: Thu, 31 May 2007 20:57:09 +0100 (BST)
>
>On Thu, 31 May 2007, Talbot Katz wrote:
>
>>Hi.
>>
>>Suppose I have a function which does some random number generation within.
>>The random number generation inside the function changes the value of
>>.Random.seed in the calling environment. If I want to restore the
> ^^^^^^^^^^^^^^^^^^^^^^^^^^
>That is your misunderstanding. From the help page
>
> The object '.Random.seed' is only looked for in the user's
> workspace.
>
>which seems plain enough. So, you can do
>
>save.seed <- get(".Random.seed", .GlobalEnv)
>assign(".Randon.seed", save.seed, .GlobalEnv)
>
>to save and restore, *provided* that random numbers have been used in the
>session (or .Random.seed will not exist).
>
>However, the help recommends using set.seed(), and why not follow the
>advice?
>
>>pre-function call .Random.seed, I can do:
>>
>>save.seed<-.Random.seed
>>result<-myfunction()
>>.Random.seed<-save.seed
>>
>>Is there a way to do the restoration inside the function? I tried putting
>>the "save.seed<-.Random.seed" and ".Random.seed<-save.seed" statements
>>inside the function, but that didn't work.
>
>As documented on the help page.
>
>[...]
>
>--
>Brian D. Ripley, ripley_at_stats.ox.ac.uk
>Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>University of Oxford, Tel: +44 1865 272861 (self)
>1 South Parks Road, +44 1865 272866 (PA)
>Oxford OX1 3TG, UK Fax: +44 1865 272595



R-help_at_stat.math.ethz.ch 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. Received on Thu 31 May 2007 - 21:24:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 May 2007 - 21:32:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.