[R] set seed for random draws

R. Michael Weylandt michael.weylandt at gmail.com
Sun Nov 6 05:27:33 CET 2011


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



More information about the R-help mailing list