question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

random.uniform documentation suggests `high` is excluded

See original GitHub issue

There’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:closed
  • Created 5 years ago
  • Comments:16 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
rkerncommented, Apr 19, 2019

Okay, let’s just document the current behavior. 😃

0reactions
mdickinsoncommented, Apr 19, 2019

Surely round-towards-minus-infinity (FE_DOWNWARD) solves this?

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 of low or high) in low + 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 forbid high from ever being produced. I’d expect that to break some corner cases in real code, so it’s probably better just to allow high to be produced in that case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found