mercredi 24 janvier 2018

Does runif() really have a range: 0<= runif(n) <= 1, as stated in the documentation?

I'm new to R, but the documentation surprised me by stating that runif(n) returns a number in the range 0 to 1 inclusive. I would expect 0 <= runif(n) < 1 -- including 0 and not including 1.

I tested it with n = 100,000,000, and found that it never produced 0 or 1. I realize that the probability of actually hitting specific values in floating point is really small, but still... (There are something like 2^53 values between 0 and 1 in double precision).

So I looked into the source code for R and found in r-source-trunk\src\nmath\runif.c

do 
{
    u = unif_rand();
} while (u <= 0 || u >= 1);
return a + (b - a) * u;

So by design, despite the documentation, it will never ever return a 0 or 1.
Isn't this a bug?

Or at least a problem with the documentation?




Aucun commentaire:

Enregistrer un commentaire