-Random numbers can be drawn on the interval (0, 1], i.e., including 1 using:
import numpy as np
r = np.random.uniform(low=0.0, high=1.0, size=10)
I, however, want to generate numbers on the interval (0, 1), i.e., NOT including 1 and 0. Something like:
r = np.random.uniform(low=0.0, high=1.0 - 2**-16, size=10)
Works, but how close can I theoretically get to 1 and 0, and is there a way to ensure that I can get as close to 1 as to 0?
First, note that np.random.uniform
returns uniformly distributed random numbers on [low, high)
, i.e., including low
but not high
. So I'm assuming you want to exclude 0
from your samples, not 1
.
In terms of how close you can practically get to zero, this is bounded by the precision of computer arithmetic. Assuming IEEE floating point numbers, as used by NumPy, the float closest to but greater than zero is given by:
>>> np.nextafter(0.0, 1.0) # Closest to 0.0 in direction of 1.0
5e-324
So to generate numbers on (0, 1)
, you can do:
>>> np.random.uniform(low=np.nextafter(0.0, 1.0), high=1.0, size=10)