[R] set.seed and for loop
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Jun 9 18:21:43 CEST 2011
On 09/06/2011 11:48 AM, jim holtman wrote:
> If you feel the need to go back and recreate a random series, then
> same the seed (.Random.seed) and restore it:
This works in this example, but wouldn't work with all RNGs, because
some of them save state outside of .Random.seed. See ?.Random.seed for
details.
Duncan Murdoch
> > set.seed(1001)
> > total<- 0
> > data<- vector("list", 30)
> > seeds<- vector("list", 30)
> > for(i in 1:30) {
> + seeds[[i]]<- .Random.seed
> + data[[i]]<- runif(50)
> + }
> >
> > .Random.seed<- seeds[[23]] # restore
> > data.23<- runif(50)
> > data.23
> [1] 0.684727876 0.592993730 0.879359238 0.454304600 0.754685981
> 0.119436749 0.527867847 0.265443455
> [9] 0.887112712 0.043309227 0.001381898 0.403483404 0.042224167
> 0.698174037 0.334604909 0.059465646
> [17] 0.374227434 0.014508142 0.265783354 0.023154917 0.668829829
> 0.184914632 0.479524914 0.644859846
> [25] 0.497644242 0.569325789 0.257636746 0.720526541 0.541526487
> 0.904469943 0.755720327 0.729912488
> [33] 0.388004197 0.940454649 0.545474130 0.285013104 0.379244716
> 0.012338111 0.192581106 0.535863633
> [41] 0.496777643 0.323488796 0.414391018 0.971135722 0.763092648
> 0.120187724 0.402572384 0.081896175
> [49] 0.303378141 0.002711767
> > data[[23]]
> [1] 0.684727876 0.592993730 0.879359238 0.454304600 0.754685981
> 0.119436749 0.527867847 0.265443455
> [9] 0.887112712 0.043309227 0.001381898 0.403483404 0.042224167
> 0.698174037 0.334604909 0.059465646
> [17] 0.374227434 0.014508142 0.265783354 0.023154917 0.668829829
> 0.184914632 0.479524914 0.644859846
> [25] 0.497644242 0.569325789 0.257636746 0.720526541 0.541526487
> 0.904469943 0.755720327 0.729912488
> [33] 0.388004197 0.940454649 0.545474130 0.285013104 0.379244716
> 0.012338111 0.192581106 0.535863633
> [41] 0.496777643 0.323488796 0.414391018 0.971135722 0.763092648
> 0.120187724 0.402572384 0.081896175
> [49] 0.303378141 0.002711767
> >
>
>
> On Thu, Jun 9, 2011 at 11:23 AM, 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.
> >
>
>
>
More information about the R-help
mailing list