[R] set seed for random draws

R. Michael Weylandt michael.weylandt at gmail.com
Sat Nov 5 01:59:52 CET 2011


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



More information about the R-help mailing list