same random numbers in different sessions
On 10/10/2010 4:30 AM, Liviu Andronic wrote:
Hello On Sun, Oct 10, 2010 at 1:16 AM, jim holtman <jholtman at gmail.com> wrote:
You need to set the set.seed yourself. There are some simulation where I do want the same numbers generated and can use the set.seed to set it to a know value. If you want something random each time, then use the time of day in the call to set.seed.
I try to do this, but I get funny results. I put try(rm(.Random.seed)) set.seed(Sys.time()) in /usr/lib/R/etc/Rprofile.site but I get the following error Error in rm(.Random.seed) : cannot remove variables from base namespace [Previously saved workspace restored] and it is as if the set.seed() call didn't work, since I get the same random values.
rnorm(1:10)
[1] -1.3618103 0.4241701 1.0720076 0.2208145 -0.5375314 -0.4846588 [7] 0.7576768 0.6527407 -0.6868786 0.8718527 If I do
set.seed(Sys.time()) rnorm(1:10)
[1] -0.6165650 0.6305187 -0.9316815 0.6034638 -0.8593514 -1.0243644 [7] -0.1050344 0.4408562 -0.3466161 0.4058430 manually, within the session, the seed seems to be changed as requested. Am I doing something wrong?
The Rprofile.site is being executed before the saved workspace is restored. See ?Startup for the sequence of events on startup. You could put the rm() in .First() in the saved workspace and it would do what you want. But more generally, I would say the thing you are doing wrong is saving .RData sometimes, but not consistently saving it. In my opinion it's safest to never save it; then you won't recover unexpected things from your history. But it's also safe to always save it. Then you'll get a new copy of .Random.seed saved each time. I think the q() function makes it a little too easy to do what you did: if you intend to never save it, but answer "Yes" just once, you get into your situation. I don't know what the alternative should be. One possibility would be for R to record whether the workspace was restored at the start of the session, and use that to determine the default when ending it. But that would mess up people who are trying to reproduce things from identical conditions, e.g. when tracking down a bug. Duncan Murdoch