Hello, all!
I need help on these two problems:
1) If I want to randomly draw numbers from standard normal (or other distributions) in loops e.g.:
ty=0; ks=0
for (i in 1:5) {
set.seed(14537+i)
k<-rnorm(1)
ks[i]<-.3*k+.9
if (ty==0) {
while ((ks<.2)||(ks>3)) {
#set.seed(13237+i*100)
k<-rnorm(1)
ks[i]-.3*k+.9 }
}
}
....
....
....
}
Question: Here I draw initial a, then if the drawn initial a satisfied 2 conditions I redraw a. I set.seed(13237) in the first draw of a, should I set.seed() in the redraw part?
2) I also have more loops after this i loop that also draw from normal(0,1). I want to randomly draws from normal(0,1) for loop j (inside loop j I draw another random numbers from N(0,1))
My question: Should I or shouldn't I set seed again and again for each loop? Why or why not.
I guess this problem concerned about setting seed as I want to have different number for each i.
Thanks!
Deana
set seed for random draws
7 messages · R. Michael Weylandt, Patrick Burns, Rolf Turner +2 more
This might be more fundamental, but why do you feel the need to reset the seed each loop? There's nothing that suggests you need to... Michael On Fri, Nov 4, 2011 at 8:38 PM, Md Desa, Zairul Nor Deana Binti
<zndeana at ku.edu> wrote:
Hello, all!
I need help on these two problems:
1) If I want to randomly draw numbers from standard normal (or other distributions) in loops e.g.:
?ty=0; ks=0
for (i in 1:5) {
? ? ? ?set.seed(14537+i)
? ? ? ?k<-rnorm(1)
? ? ? ?ks[i]<-.3*k+.9
? ? ? ?if (ty==0) {
? ? ? ? ? ?while ((ks<.2)||(ks>3)) {
? ? ? ? ? ?#set.seed(13237+i*100)
? ? ? ? ? ?k<-rnorm(1)
? ? ? ? ? ?ks[i]-.3*k+.9 }
? ? ? ?}
? ? }
....
....
....
? ? }
Question: Here I draw initial a, then if the drawn initial a satisfied 2 conditions I redraw a. I set.seed(13237) in the first draw of a, should I set.seed() in the redraw part?
2) I also have more loops after this i loop that also draw from normal(0,1). I want to randomly draws from normal(0,1) for loop j (inside loop j I draw another random numbers from N(0,1))
My question: Should I or shouldn't I set seed again and again for each loop? Why or why not.
I guess this problem concerned about setting seed as I want to have different number for each i.
Thanks!
Deana
______________________________________________ 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.
I'm suspecting this is confusion about default behavior. R automatically updates the random seed when random numbers are generated (or other random operations are performed). The original poster may have experienced systems where it is up to the user to change the seed. I'd suggest two rules of thumb when coming up against something in R that you aren't sure about: 1. If it is a mundane task, R probably takes care of it. 2. Experiment to see what happens. Of course you could read documentation, but no one does that.
On 05/11/2011 00:59, R. Michael Weylandt wrote:
This might be more fundamental, but why do you feel the need to reset the seed each loop? There's nothing that suggests you need to... Michael On Fri, Nov 4, 2011 at 8:38 PM, Md Desa, Zairul Nor Deana Binti <zndeana at ku.edu> wrote:
Hello, all!
I need help on these two problems:
1) If I want to randomly draw numbers from standard normal (or other distributions) in loops e.g.:
ty=0; ks=0
for (i in 1:5) {
set.seed(14537+i)
k<-rnorm(1)
ks[i]<-.3*k+.9
if (ty==0) {
while ((ks<.2)||(ks>3)) {
#set.seed(13237+i*100)
k<-rnorm(1)
ks[i]-.3*k+.9 }
}
}
....
....
....
}
Question: Here I draw initial a, then if the drawn initial a satisfied 2 conditions I redraw a. I set.seed(13237) in the first draw of a, should I set.seed() in the redraw part?
2) I also have more loops after this i loop that also draw from normal(0,1). I want to randomly draws from normal(0,1) for loop j (inside loop j I draw another random numbers from N(0,1))
My question: Should I or shouldn't I set seed again and again for each loop? Why or why not.
I guess this problem concerned about setting seed as I want to have different number for each i.
Thanks!
Deana
______________________________________________ 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.
Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
On 05/11/11 22:00, Patrick Burns wrote:
<SNIP>
I'd suggest two rules of thumb when coming up against something in R that you aren't sure about: 1. If it is a mundane task, R probably takes care of it. 2. Experiment to see what happens. Of course you could read documentation, but no one does that.
<SNIP>
Fortune nomination!
cheers,
Rolf
Thank you everybody for the helpful advices. Basically, I try to figure out why I get different numbers as there are more than one seed for a loop within a loop. Well, I guest I got it now. Because every time random seed is called or specified it'll output different random numbers, as it's requested. Thanks! D
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf of Rolf Turner [rolf.turner at xtra.co.nz]
Sent: Saturday, November 05, 2011 3:22 PM
To: Patrick Burns
Cc: r-help at r-project.org; Achim.Zeileis at uibk.ac.at
Subject: Re: [R] set seed for random draws
Sent: Saturday, November 05, 2011 3:22 PM
To: Patrick Burns
Cc: r-help at r-project.org; Achim.Zeileis at uibk.ac.at
Subject: Re: [R] set seed for random draws
On 05/11/11 22:00, Patrick Burns wrote:
<SNIP>
> I'd suggest two rules of thumb when coming
> up against something in R that you aren't
> sure about:
>
> 1. If it is a mundane task, R probably
> takes care of it.
>
> 2. Experiment to see what happens.
>
>
> Of course you could read documentation, but
> no one does that.
<SNIP>
Fortune nomination!
cheers,
Rolf
______________________________________________
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.
I think it's easier than you are making it: the random seed is created in a "pretty-random" way when you first use it and then it is updated with each call to rDIST(). For example, set.seed(1) x1 <- .Random.seed rnorm(1) x2 <- .Random.seed rnorm(1) x3 <- .Random.seed identical(x1, x2) FALSE identical(x1, x3) FALSE identical(x2, x3) FALSE set.seed(1) identical(x1, .Random.seed) TRUE rnorm(2) identical(x3, .Random.seed) TRUE But the period for the random seed to repeat is very, very long so you don't have to think about it unless you really need to (or for reproducible simulations) Michael On Sat, Nov 5, 2011 at 7:22 PM, Md Desa, Zairul Nor Deana Binti
<zndeana at ku.edu> wrote:
Thank you everybody for the helpful advices. Basically, I try to figure out why I get different numbers as there are more than one seed for a loop within a loop. Well, I guest I got it now. Because every time random seed is called or specified it'll output different random numbers, as it's requested. Thanks! D
________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf of Rolf Turner [rolf.turner at xtra.co.nz] Sent: Saturday, November 05, 2011 3:22 PM To: Patrick Burns Cc: r-help at r-project.org; Achim.Zeileis at uibk.ac.at Subject: Re: [R] set seed for random draws On 05/11/11 22:00, Patrick Burns wrote: <SNIP> I'd suggest two rules of thumb when coming up against something in R that you aren't sure about: 1. If it is a mundane task, R probably takes care of it. 2. Experiment to see what happens. Of course you could read documentation, but no one does that. <SNIP> Fortune nomination! ? ? cheers, ? ? ? ? Rolf ______________________________________________ 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.
On Sun, 6 Nov 2011, R. Michael Weylandt wrote:
I think it's easier than you are making it: the random seed is created in a "pretty-random" way when you first use it and then it is updated
Ah: It is unless you then save the workspace. If you do, then evey subsequent session starts with the same seed until you save the workspace again. So never saving and always saving works fine, but occasional saving can lead to puzzlement. That "pretty-random" way is as unpredictable as pseado-random numbers (It uses a PRNG internally.)
with each call to rDIST(). For example, set.seed(1) x1 <- .Random.seed rnorm(1) x2 <- .Random.seed rnorm(1) x3 <- .Random.seed identical(x1, x2) FALSE identical(x1, x3) FALSE identical(x2, x3) FALSE set.seed(1) identical(x1, .Random.seed) TRUE rnorm(2) identical(x3, .Random.seed) TRUE But the period for the random seed to repeat is very, very long so you don't have to think about it unless you really need to (or for reproducible simulations) Michael On Sat, Nov 5, 2011 at 7:22 PM, Md Desa, Zairul Nor Deana Binti <zndeana at ku.edu> wrote:
Thank you everybody for the helpful advices. Basically, I try to figure out why I get different numbers as there are more than one seed for a loop within a loop. Well, I guest I got it now. Because every time random seed is called or specified it'll output different random numbers, as it's requested. Thanks! D
________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf of Rolf Turner [rolf.turner at xtra.co.nz] Sent: Saturday, November 05, 2011 3:22 PM To: Patrick Burns Cc: r-help at r-project.org; Achim.Zeileis at uibk.ac.at Subject: Re: [R] set seed for random draws On 05/11/11 22:00, Patrick Burns wrote: <SNIP> I'd suggest two rules of thumb when coming up against something in R that you aren't sure about: 1. If it is a mundane task, R probably takes care of it. 2. Experiment to see what happens. Of course you could read documentation, but no one does that. <SNIP> Fortune nomination! ? ? cheers, ? ? ? ? Rolf ______________________________________________ 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.
______________________________________________ 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