[R] Correctly Setting New Seed

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed May 8 22:36:48 CEST 2013


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ellerbe, Caitlyn Nicole
> Sent: Wednesday, May 08, 2013 11:51 AM
> To: Ellerbe, Caitlyn Nicole; r-help at R-project.org
> Subject: Re: [R] Correctly Setting New Seed
> 
> By crash, I mean that it shuts the program and no error message is
> provided. However, my question is more general - how to get the two
> versions of code below to return the same string of random numbers. The
> code provided will run without incident  and is only provided to make
> the problem clear. The true code that causes the crash isn't necessary,
> only that I need to be able to split it into smaller chunks while
> maintaining the integrity of the random sequence even if the program
> closes and all information is lost.
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ellerbe, Caitlyn Nicole
> Sent: Wednesday, May 08, 2013 2:06 PM
> To: r-help at R-project.org
> Subject: [R] Correctly Setting New Seed
> 
> Could someone please suggest a method to store the current random seed.
> I'm having trouble understanding how to correctly use set.seed and
> .Random.seed.
> 
> Specifically, I have the following code that crashes:
> 
> 
> 
> set.seed(seed)
> 
> for(i in 1:10){
> 
>   print( runif(1))
> 
> }
> 
> 
> 
> To get around this I need to split the number of iterations into
> chunks:
> 
> 
> 
> set.seed(seed)
> 
> for(i in 1:5){
> 
> print(runif(1))
> 
> new.seed<-.Random.seed
> 
> }
> 
> 
> 
> set.seed(new.seed)
> 
> for(i in 6:10){
> 
> print(runif(1))
> 
> }
> 
> 
> 
> When I compare the sequence of numbers from the single run to the
> sequence from the chunked code they don't match. Is the .Random.seed
> argument in the wrong position or is there another way to accomplish
> this?
> 

To answer your specific question, you need to change the line

set.seed(new.seed)

To 

.Random.seed <- new.seed


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list