[R] set.seed and for loop

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Jun 9 18:25:48 CEST 2011



On 09.06.2011 18:09, R Help wrote:
> That wouldn't work because the seed is for the first iteration.
>
> Random numbers are generated by a seed, after which the seed changes
> (I don't know the mechanism for changing the seed in R, but it's
> static)
>
> That means that, if you set the seed to 1001, and then run runif
> function 50 times, you'll get 50 different sets of random numbers.  If
> you reset the seed to 1001 and then run runif again, the result will
> be the same as data[[1]], not [[23]].  And you can't just set the seed
> to 1023 because that's not how the seed changes.
>
> I think Jim's suggestion was the best.  I was thinking of that but I
> couldn't remember how to extract the seed.


Well set.seed() actually generates a seed. See ?set.seed that points us 
to .Random.seed (and relevant references!) which contains the actual 
current seed.

Uwe Ligges



> Sam
>
> On Thu, Jun 9, 2011 at 12:23 PM, Samuel Le<Samuel.Le at srlglobal.com>  wrote:
>> What about:
>> set.seed(1001)
>> total<- 0
>> data<- vector("list", 30)
>> for(i in 1:30) {
>>   data[[i]]<- runif(50)
>> }
>> set.seed(1001)
>> data[[23]]<- runif(50)
>>
>> HTH
>> Samuel
>>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Soyeon Kim
>> Sent: 09 June 2011 16:15
>> To: r-help
>> Subject: [R] set.seed and for loop
>>
>> Dear All,
>>
>> This is hard to describe so I made a simple example.
>> set.seed(1001)
>> total<- 0
>> data<- vector("list", 30)
>> for(i in 1:30) {
>>   data[[i]]<- runif(50)
>> }
>> Let's call a data set runif(50).
>> While the for loop is running, 100 data sets  are generated.
>> I want to restore 23th data set (the data set generated in 23th for
>> loop) without the loop.
>> I've tried set.seed(1023) runif(50)
>> but this is different data from the data set gotten from 23th for loop.
>> How can I get 23th data set without the loop?
>>
>> Thank you,
>> Soyeon
>>
>> ______________________________________________
>> 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.
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus signature database 6193 (20110609) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus signature database 6193 (20110609) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.



More information about the R-help mailing list