Davis Yoshida
Davis Yoshida

Reputation: 1785

Why does numpy use the half-open interval for its uniform random?

All of numpy's random functions say things like:

Create an array of the given shape and propagate it with random samples from a uniform distribution over [0, 1).

(See here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand)

What is the reason for using the half-open interval [0, 1)? From a probabalistic point of view, it shouldn't matter whether 1 is included or not.

Upvotes: 5

Views: 3997

Answers (1)

roadrunner66
roadrunner66

Reputation: 7941

With arbitrary precision it would indeed not matter, as the probability of reaching any given real number would be zero (non-zero only for an interval).

Computationally it does matter, since you use finite numerical resolution (e.g. double numbers). So every interval is effectively a closed interval.

Using half-open intervals by default allows you to avoid problems if you stack intervals. So [0,1) and [1,2) will not have common numbers.

For achieving open-open intervals and other concerns see e.g. this other stackoverflow question

Upvotes: 5

Related Questions