Re: [R] difference between rnorm(1000, 0, 1) and running rnorm(5

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Thu 09 Feb 2006 - 00:26:13 EST


On 08-Feb-06 Duncan Murdoch wrote:
> On 2/8/2006 4:53 AM, Bjørn-Helge Mevik wrote:

>> Why don't you test it yourself?
>> 
>> E.g.,
>> 
>> set.seed(42)
>> bob1 <- rnorm(1000,0,1)
>> set.seed(42)
>> bob2 <- rnorm(500,0,1)
>> bob3 <- rnorm(500,0,1)
>> identical(bob1, c(bob2, bob3))
>> 
>> I won't tell you the answer. :-)

>
> This isn't really something that can be proved by a test. Perhaps the
> current implementation makes those equal only because 500 is even, or
> divisible by 5, or whatever...
>
> I think the intention is that those should be equal, but in a quick
> search I've been unable to find a documented guarantee of that. So I
> would take a defensive stance and assume that there may be conditions
> where c(rnorm(m), rnorm(n)) is not equal to rnorm(m+n).
>
> If someone can point out the document I missed, I'd appreciate it.
>
> Duncan Murdoch

On my understanding, once the seed is set the sequence generated by the underlying RNG is determined, whether it is the result of a single call to produce a long sequence or multiple calls to generate many shorter sequences. Example:

> set.seed(42)
> multi<-numeric(20)
> set.seed(42)
> single<-rnorm(20)
> set.seed(42)
> for(i in (1:20)) multi[i]<-rnorm(1)
> print(max(multi-single),digits=22)

[1] 0
> print(min(multi-single),digits=22)

[1] 0

In other words: identical!

Whether there are possible exceptions, in some implementations of r<dist> where <dist> is other than "norm", has to be answered by people who are familiar with the internals of these functions.

Best wishes to all,
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 08-Feb-06                                       Time: 13:26:10
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Feb 09 00:42:30 2006

This archive was generated by hypermail 2.1.8 : Fri 10 Feb 2006 - 21:35:08 EST