Re: [R] Random number generation

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri 02 Jun 2006 - 05:52:42 EST

On 6/1/2006 3:13 PM, Carl wrote:
> Hi All.
> (This is probably an incredibly stupid question :))
>
> I have just noticed that every time I start an R2.3 session and request
> a random number (say with rnorm) that I receive the same number. AFAIK I
> have not set a seed at any point. Returning to an older version of
> R(v1.8) I get the behaviour that I have come to expect, where a new seed
> is automatically generated for each new session (actually it was a
> student running bootstraps on multiple machines that has noticed this).
>
> I am running R2.3 for windows on winXP pro, AMD Athlon 2400+.

R saves the random seed in a variable called .Random.seed. (Because of the initial dot, this is not normally shown by ls(). Use ls(all=T) to see it.

If you save this to an .RData file, it will be reloaded next time you run, and your RNG will continue on from before.

I'm guessing you saved a copy once, but are *not* saving your workspace each time you restart: hence you get the same seed each time.

You can avoid this by deleting the saved workspace, or just deleting .Random.seed and saving it (but it will be resurrected next time you do a save), or by always saving your workspace (so you save a different seed every time).

My advice would be not to save the workspace; I save individual objects sometimes, but I like a nice clean workspace each time I start.

Duncan Murdoch



R-help@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 Received on Fri Jun 02 06:00:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 02 Jun 2006 - 08:10:46 EST.

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