random.uniform documentation suggests `high` is excluded
See original GitHub issueThere’s a possibility of confusion from the np.random.uniform
documentation, which says:
Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).
Arguably, that’s true, in the sense that we’re using an expression that (mathematically) draws from [low, high)
, but rounding and numerical errors mean that in practice, high
can be generated:
>>> low = 1.0
>>> high = 1.0 + 2**-40
>>> low, high, low < high
(1.0, 1.0000000000009095, True)
>>> np.random.uniform(low, high, size=10**6).max() == high
True
The current documentation leads to people (well, at least one SO user) writing things like np.random.uniform(low, np.nextafter(high, np.inf))
when they want a closed interval, which probably isn’t a good idea:
>>> np.random.uniform(low, np.nextafter(high, np.inf), size=10**6).max() > high
True
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (13 by maintainers)
Top Results From Across the Web
random — Generate pseudo-random numbers — Python 3.11 ...
This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is ...
Read more >How to create a random Uniform Distribution between (but ...
Hello, How can I create a random uniform distribution between 0 (which is excluded) and 10 (included). That is, the range is between...
Read more >In python, what is the difference between random.uniform ...
They both generate pseudo random numbers, random.uniform() generates numbers from a uniform distribution and random.random() generates the next random number.
Read more >numpy.random.uniform — NumPy v1.24 Manual
Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high).
Read more >9.6. random — Generate pseudo-random numbers
For sequences, there is uniform selection of a random element, ... method — this allows randrange() to produce selections over an arbitrarily large...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, let’s just document the current behavior. 😃
Ah yes, true; I was (wrongly) thinking in terms of round-towards-zero. Sorry. So for
low < high
, with round-towards-negative-infinity, yes, all three rounds go the right way (regardless of the sign oflow
orhigh
) inlow + random() * (high - low)
.For
low > high
(which is currently supported), you’d need to be a bit more careful to make sure everything rounds the right way. I think- (random() * (low - high) - low)
should do it, again using FE_DOWNWARD.
For
low == high
(which is also currently supported), you’d need to make that an error if you want to forbidhigh
from ever being produced. I’d expect that to break some corner cases in real code, so it’s probably better just to allowhigh
to be produced in that case.