3
$\begingroup$

I'd like to generate a new point which should be uniformly distributed on the interval [a, b) (i.e. including the left extreme value - a and exluding the right extreme value - b). The runif help says:

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.

As I understand it, it generates values on the interval (a, b). Does this make any difference?

$\endgroup$
  • 1
    $\begingroup$ May I ask, why do you care in the first place? The probability of observing the left endpoint is zero. $\endgroup$ – cardinal May 1 '12 at 2:08
  • $\begingroup$ That's what I thought as well, but then I wasn't sure if it really doesn't make any difference. $\endgroup$ – John Manak May 1 '12 at 2:12
  • 2
    $\begingroup$ The situation in which you would care is in evaluating certain numerical pathologies that arise due to actually observing a boundary value or not. Take for example the generation of an exponential random variate with rate one. This is obtained as either $-\log U$ or $-\log(1-U)$ where $U$ is a standard uniform. In the first case you'd definitely hate to see zero pop out of your RNG. Likewise, in the second case you wouldn't be very pleased with it returning 1. $\endgroup$ – cardinal May 1 '12 at 2:17
  • 1
    $\begingroup$ I largely agree w/ @cardinal's comments, but I would put it slightly differently. You ask "Does this make any difference?" Does it make a difference for what exactly? What are your goals here? You want to generate data on the interval [a,b), that's pretty specific, what hinges on "[a" vs "(a" for what you are trying to do? $\endgroup$ – gung - Reinstate Monica May 1 '12 at 3:33
  • 1
    $\begingroup$ According to the observation by @Procrastinator, here is a working, correct R implementation of a uniform variable supported on $[a,b)$ (not just $(a,b)$): function(n, min=0, ...) {t <- runif(n, min=min, ...); if(FALSE) min else t} :-). $\endgroup$ – whuber May 1 '12 at 16:10
2
$\begingroup$

At this point, perhaps we can consolidate the comments above into an answer. If you agree with the point made by @cardinal and @Procrastinator that the probability of finding a value at your endpoint is 0, and you are not concerned about this (it doesn't matter to you that you won't get this value), we can set @whuber's code aside, and just recommend runif().

Since I'm mostly repackaging ideas from several people here, I am marking this 'community wiki' (i.e., I don't need to take credit for it).

Let me know if you have any other concerns.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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