[R] random sequences for rnorm and runif

Matthias Gondan matthias-gondan at gmx.de
Thu Feb 3 14:14:34 CET 2011


Dear R experts,

For a fixed seed, the first random number produced by rnorm and runif 
has the same rank within the distribution, which I find useful. The 
following ranks differ, however.

> set.seed(123)
> runif(4)
[1] *0.2875775* 0.7883051 *0.4089769* 0.8830174

> set.seed(123)
> pnorm(rnorm(4))
[1] 0.2875775 0.4089769 0.9404673 0.5281055

I noticed that rnorm seems to 'eat' two seeds of the random
number generator, whereas runif eats only one seed. Is this 
intended behavior or do you think it should be fixed? 

The strange thing is that the 1st/3rd/5th etc number of rnorm 
corresponds to the 1st/2nd/3rd in runif. If two seeds are necessary, 
I would have expected the following correspondence, 2-1, 4-2, 6-3,
etc.

Temporary fix:

> myrnorm = function(n, mean=0, sd=1)
+ {
+     qnorm(runif(n), mean, sd)
+ } # myrnorm
> 
> set.seed(123)
> pnorm(myrnorm(4))
[1] 0.2875775 0.7883051 0.4089769 0.8830174

Best wishes,

Matthias Gondan
-- 
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit 
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl



More information about the R-help mailing list