[R] How to stop set.seed() besides exiting out of R?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 18 21:22:20 CET 2013


On 18/03/2013 19:59, William Dunlap wrote:
> I am not sure of this, but I think you can unset the seed by removing the dataset .Random.seed
> from the global environment.  E.g.,
>
>> set.seed(1)
>> runif(5)
> [1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819
>> rm(list=".Random.seed", envir=globalenv())
>> runif(5)
> [1] 0.5952379 0.3355091 0.8820192 0.7633754 0.8064312
>>
>>
>> set.seed(1)
>> runif(5) # same as before
> [1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819
>> rm(list=".Random.seed", envir=globalenv())
>> runif(5) # different
> [1] 0.52023610 0.73407695 0.08824484 0.26977430 0.80089250

Yes, almost all the time.  R does keep a copy in memory when it is using 
it and writes it back out when done with it: so assuming nothing 
asynchronous is going on (e.g. a GUI callback) removing .Random.seed 
will cause the RNG to be re-initialized at next use.

>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
>> Of Nordlund, Dan (DSHS/RDA)
>> Sent: Monday, March 18, 2013 12:44 PM
>> To: C W
>> Cc: r-help
>> Subject: Re: [R] How to stop set.seed() besides exiting out of R?
>>
>> No, you cannot unset the seed.  You can set it to a different value, but a the random
>> number generators always need a starting seed.  If you don’t set one, R will set one for
>> you , you just won’t know what it is.  And as a practical matter, given a sequence of
>> random numbers you can’t tell what the starting seed was.  That is the point of good
>> random number generators.  Each sequence of random numbers for most intents and
>> purposes can be considered independent from previous sets of numbers.
>>
>> 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
>>
>> From: C W [mailto:tmrsg11 at gmail.com]
>> Sent: Monday, March 18, 2013 12:19 PM
>> To: Nordlund, Dan (DSHS/RDA)
>> Cc: r-help
>> Subject: Re: [R] How to stop set.seed() besides exiting out of R?
>>
>> Yes, I agree with you.  I guess what I was really looking for is a function like
>> UNset.seed()?
>>
>> By having set.seed(), I can have reproducible code.  But what if I want to check my work
>> against what's produced from set.seed(100)?
>>
>> I really want to escape from the shadow of set.seed(), can I unset it?
>> On Mon, Mar 18, 2013 at 3:07 PM, Nordlund, Dan (DSHS/RDA)
>> <NordlDJ at dshs.wa.gov<mailto:NordlDJ at dshs.wa.gov>> wrote:
>> As I understand it, how R “‘normally” does it is to use the system clock to set the seed
>> once per session, unless you use set.seed() to set a new seed. You chose to set the seed
>> to a different value.  But from that point on, the pseudo random number generation
>> continues  in the same way it “normally” does.  In your code below, each of your 100
>> histograms will be different.  If you then execute the for loop again (but not the
>> set.seed(100) statement), you will get a different set of histograms.  The only way you
>> would be “confined to set.seed(100)” is if you keep resetting the seed to 100.
>>
>> 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
>> From: C W [mailto:tmrsg11 at gmail.com<mailto:tmrsg11 at gmail.com>]
>> Sent: Monday, March 18, 2013 11:50 AM
>> To: Nordlund, Dan (DSHS/RDA)
>> Cc: r-help
>> Subject: Re: [R] How to stop set.seed() besides exiting out of R?
>>
>> set.seed(100)
>> for (i in 1:100){
>>      a <- rnorm(1000, mean=0, sd=1)
>>      hist(a)
>> }
>>
>> #Now say, I want to simulate without being confined to set.seed(100), I just want to get
>> a simulation like how R "normally" does it.
>>
>> Mike
>> On Mon, Mar 18, 2013 at 2:40 PM, Nordlund, Dan (DSHS/RDA)
>> <NordlDJ at dshs.wa.gov<mailto:NordlDJ at dshs.wa.gov><mailto:NordlDJ at dshs.wa.gov<
>> mailto:NordlDJ at dshs.wa.gov>>> wrote:
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.org><mailto:r-
>> help-bounces at r-project.org<mailto:r-help-bounces at r-project.org>> [mailto:r-help-
>> bounces at r-<mailto:r-help-bounces at r-><mailto:r-help-bounces at r-<mailto:r-help-
>> bounces at r->>
>>> project.org<http://project.org><http://project.org>] On Behalf Of C W
>>> Sent: Monday, March 18, 2013 11:27 AM
>>> To: r-help
>>> Subject: [R] How to stop set.seed() besides exiting out of R?
>>>
>>> Hi list,
>>>
>>> I am curious how to stop the set.seed(), I don't want the same repeated
>>> random number.  I know I can set it to a different seed, but I don't
>>> want
>>> to go through the trouble of setting different seed every time.
>>>
>>> Thanks,
>>> Mike
>>>
>> Can you show us how you are using set.seed() that results in getting the same sequence
>> repeatedly?  If you are doing simulations in a loop, then set the seed once, outside the
>> loop.  Otherwise, I am not sure what you are doing that causes problems.  A reproducible
>> example would really help.
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org<mailto:R-help at r-project.org><mailto:R-help at r-
>> project.org<mailto:R-help at r-project.org>> mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>          [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help at r-project.org<mailto:R-help at r-project.org> mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
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