set.seed and for loop

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

set.seed and for loop

Soyeon Kim-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Duncan Murdoch-2
On 09/06/2011 11:14 AM, Soyeon Kim wrote:

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

You can't.

To get the 23rd value again, set the seed to 1001, run the loop 22
times, then the next one will be the 23rd.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Samuel Le
In reply to this post by Soyeon Kim-2
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: [hidden email] [mailto:[hidden email]] 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

______________________________________________
[hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

R Help
In reply to this post by Soyeon Kim-2
There are certainly people that would know how the random functions
work better than I, but I believe you would need to simulate 22
datasets and then get the 23rd dataset.  So to restore the 23rd:

set.seed(1001)
for(i in 1:22){
  garbage = runif(50)
}
data[[23]] = runif(50)

Hope that helps,
Sam

On Thu, Jun 9, 2011 at 12:14 PM, Soyeon Kim <[hidden email]> wrote:

> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

jholtman
In reply to this post by Samuel Le
If you feel the need to go back and recreate a random series, then
same the seed (.Random.seed) and restore it:

> 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 <[hidden email]> 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: [hidden email] [mailto:[hidden email]] 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
>
> ______________________________________________
> [hidden email] 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
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

R Help
In reply to this post by Samuel Le
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.

Sam

On Thu, Jun 9, 2011 at 12:23 PM, Samuel Le <[hidden email]> 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: [hidden email] [mailto:[hidden email]] 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
>
> ______________________________________________
> [hidden email] 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Duncan Murdoch-2
In reply to this post by jholtman
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<[hidden email]>  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: [hidden email] [mailto:[hidden email]] 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
> >
> >  ______________________________________________
> >  [hidden email] 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
> >
> >  ______________________________________________
> >  [hidden email] 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.
> >
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Duncan Murdoch-2
In reply to this post by jholtman
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<[hidden email]>  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: [hidden email] [mailto:[hidden email]] 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
> >
> >  ______________________________________________
> >  [hidden email] 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
> >
> >  ______________________________________________
> >  [hidden email] 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.
> >
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Uwe Ligges-3
In reply to this post by R Help


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<[hidden email]>  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: [hidden email] [mailto:[hidden email]] 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
>>
>> ______________________________________________
>> [hidden email] 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
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: set.seed and for loop

Petr Savicky-2
In reply to this post by Soyeon Kim-2
On Thu, Jun 09, 2011 at 10:14:48AM -0500, Soyeon Kim wrote:

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

It is possible to save a bit (not much) over the loop,
since a sequence of calls of runif() creates the numbers
from the same sequence of numbers. So it is possible to
get more numbers in one call not changing the rest of
the sequence.

I mean the following

  set.seed(1001)
  total <- 0
  data <- vector("list", 30)
  for(i in 1:30) {
    data[[i]] <- runif(50)
  }
  set.seed(1001)
  garbage <- runif(22*50)
  recomp <- runif(50)
  identical(data[[23]], recomp)

  [1] TRUE

There are algorithms for jumping ahead in the sequence
without generating all intermediate numbers, but i do
not know about an efficient available implementation.

Petr Savicky.

______________________________________________
[hidden email] 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.