[R] reading the seed from a simulation

Tony Plate tplate at blackmesacapital.com
Fri Dec 17 18:39:59 CET 2004


With most modern random number generators you can't capture the current 
state in a single 32-bit integer.  (I suspect the .Random.seed you are 
seeing is the state contained in 625 integers).

The easiest way to run reproducible simulations is to explicitly set the 
seed, using an integer, before each run.  Then it's easy to put the random 
number generator into the same state again, e.g.:

for (sim.num in 1:100) {
   set.seed(sim.num)
   ... run simulation ...
}

If you can't do this, you can record the value of .Random.seed prior to the 
simulation, and then when you want to reproduce that simulation again, set 
.Random.seed to that value, e.g.:

 > set.seed(1)
 > sample(1:100, 5)
[1] 27 37 57 89 20
 > sample(1:100, 5)
[1] 90 94 65 62  6
 > set.seed(1)
 > sample(1:100, 5)
[1] 27 37 57 89 20
 > saved.seed <- .Random.seed
 > sample(1:100, 5)
[1] 90 94 65 62  6
 > .Random.seed <- saved.seed
 > sample(1:100, 5)
[1] 90 94 65 62  6
 >

This is not guaranteed to work with all random-number generators; see the 
NOTE section in ?set.seed

-- Tony Plate


At Friday 09:50 AM 12/17/2004, Suzette Blanchard wrote:

>Greetings,
>
>         I have a simulation of a nonlinear model that
>is failing.  But it does not fail til way into the simulation.
>I would like to look at the run that is failing
>and maybe I could if I could capture the seed for the
>failing run.  The help file on set.seed says you can do it
>but when I tried
>
>rs<-.Random.seed
>print(paste("rs",rs,sep=" "))
>
>I got 626 of them so I don't know how to identify the right
>one.  Please can you help?
>
>Thank you,
>Suzette
>
>=================================
>Suzette Blanchard, Ph.D.
>UCSD-PPRU
>
>______________________________________________
>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




More information about the R-help mailing list