Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Enforce high >= low on uniform number generators #17921

Merged
merged 2 commits into from Dec 14, 2020

Conversation

bashtage
Copy link
Contributor

@bashtage bashtage commented Dec 4, 2020

Check that high is weakly larger than low and raise if now

closes #17905

@seberg seberg added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Dec 4, 2020
@rkern
Copy link
Member

rkern commented Dec 5, 2020

I don't think this qualifies for an exception to the freezing of RandomState per NEP 19. I think this should be confined to Generator.

Please update the Notes section which still refers to the old behavior.

@bashtage
Copy link
Contributor Author

bashtage commented Dec 7, 2020

I agree with @rkern that we cannot change mtrand.pyx in this instance, and so the PR is limited to the new generator. It adds a check. An alternative would be to better explain that role of high low and make this the default behaviour. Something along the lines of

The input values are not checked for order. The uniform is generated as low + (high - low) * random(), so that if
high < low, then the uniform is generated as if high and low are swaped.

Probably simpler to enforce though. Probably will also help to avoid hard to detect bugs by end-users.

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I am happy to put this in then. Two small things:

  1. I guess it may be worth adding a release note, since it is a compatibility change in theory? (even if the new API isn't very old anyway)
  2. Just curious what happens/should happen for low == high, and if we should test that.

func = random.uniform
assert_raises(ValueError, func, 2, 1)
assert_raises(ValueError, func, [1, 2], [1, 1])
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_raises(ValueError, func, [[0, 1],[2, 3]], 2)
assert_raises(ValueError, func, [[0, 1], [2, 3]], 2)

@rkern
Copy link
Member

rkern commented Dec 7, 2020

2. Just curious what happens/should happen for low == high, and if we should test that.

That case is documented, if not tested. It always returns the constant low (or equivalently, high).

@bashtage
Copy link
Contributor Author

bashtage commented Dec 7, 2020 via email

Kevin Sheppard added 2 commits December 11, 2020 15:27
Check that high is weakly larger than low and raise if now

closes numpy#17905
This doesn't qualify for fixing under the NEP.
@charris
Copy link
Member

charris commented Dec 13, 2020

What about integers? There is #14333, which has other problems, but it seems somewhat related.

@bashtage
Copy link
Contributor Author

What about integers? There is #14333, which has other problems, but it seems somewhat related.

#14333 is mostly a preference about the error message. This is about whether this is high > low is actually an error.

@mattip mattip merged commit e4feb70 into numpy:master Dec 14, 2020
@mattip
Copy link
Member

mattip commented Dec 14, 2020

Thanks @bashtage

@charris charris removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Dec 14, 2020
@eric-wieser
Copy link
Member

With this change, it's now harder to generate a random number in the half-open interval (low, high], which I get the feeling was what passing the arguments in the "wrong" order did. Are we sure it wasn't better to support that use-case?

@bashtage
Copy link
Contributor Author

With this change, it's now harder to generate a random number in the half-open interval (low, high], which I get the feeling was what passing the arguments in the "wrong" order did. Are we sure it wasn't better to support that use-case?

That feels like a magic incarnation, at least given the lack of clear intent for this case doc. It doesn't seem to be that much larger to do what you wish.

-gen.uniform(-high, -low)

@rkern
Copy link
Member

rkern commented Dec 14, 2020

With floating point arithmetic, there are very no real guarantees that you'll actually get a half-open interval, on either side.

@eric-wieser
Copy link
Member

there are very no real guarantees that you'll actually get a half-open interval, on either side.

Ah, I was misled by the docstring which seems to suggest the interval is truly half-open, only to remark in the notes that this is not guaranteed. Sounds like my concern isn't relevant, thanks for clarifying.

@rkern
Copy link
Member

rkern commented Dec 14, 2020

There's definitely room for a uniform-like method that fully controls whether the ends are open or closed, in any combination. But it should also enforce low <= high and control the open/closed-ness through other arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants