1
$\begingroup$

The title says it all. Can it happen that runif (with bounds 0 and 1) returns 0 or 1 in R?

$\endgroup$
1
  • 7
    $\begingroup$ This answer on Stack Echange is detailed enough to make this question a duplicate. $\endgroup$
    – Xi'an
    Commented Mar 24, 2020 at 6:54

1 Answer 1

9
$\begingroup$

It sounds like you're already aware that drawing a 0 or 1 from the standard uniform distribution is a zero-probability event. Instead, as I understand it your question is specifically about R, and whether runif will return 0 or 1 due to the finite numerical precision of your computer. We can read the following from ?runif:

runif will not generate either of the extreme values unless max = min or max-min is small compared to min, and in particular not for the default arguments.

So we conclude that drawing from the standard uniform distribution in R using runif will never return 0 or 1. You are safe to, for instance, log-transform the result without worrying about whether you will obtain finite values.

$\endgroup$
1
  • 5
    $\begingroup$ Re your last sentence: most people are perfectly content with finite values; its the other kind that worry them... $\endgroup$
    – Glen_b
    Commented Mar 24, 2020 at 4:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.