[R] Restoring .Random.seed

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 31 21:57:09 CEST 2007

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

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 

> 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

More information about the R-help mailing list