gh-149221:Fix binomialvariate Function for random module#149222
gh-149221:Fix binomialvariate Function for random module#149222lighting9999 wants to merge 20 commits intopython:mainfrom
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
…y.rst Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @rhettinger: please review the changes made to this pull request. |
skirpichev
left a comment
There was a problem hiding this comment.
I think it's not too hard to find a seed, that trigger an exception. Maybe issue worth a test?
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Documentation build overview
30 files changed ·
|
# Test for pythongh-149222: binomialvariate should not crash when random()
|
ok @skirpichev ,Please review. |
| if c < 0: | ||
| # When c < 0, log(0) mathematically gives +infinity, so y > n. | ||
| y = n + 1 |
There was a problem hiding this comment.
I think you can do a quick exit:
| if c < 0: | |
| # When c < 0, log(0) mathematically gives +infinity, so y > n. | |
| y = n + 1 | |
| return x |
Remember, c < 0 at this point.
|
@skirpichev Can you let me focus on this one? We're pushing the OP in two different directions. |
| @@ -0,0 +1,2 @@ | |||
| Fix rare math domain error for :func:`random.binomialvariate`, using | |||
| y += _floor(_log2(random()) / c) + 1 | ||
| except ValueError: | ||
| # Handle rare case of log(0.0) | ||
| if c < 0: |
There was a problem hiding this comment.
Please just turn the handler into a continue for a reselection. Jumping to infinity isn't desirable. I don't want to distort the distribution.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sure, @rhettinger, sorry for the noise. Though, I think the only difference is that you suggest to use "continue" statement instead of "return x". |
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Removed unnecessary try-except block for log(0.0) handling.
|
This is all that is needed: |
What I originally had in mind was this: u = random()
if u == 0.0:
continue
y += _floor(_log2(u) / c) + 1No try-except, just a simple check and skip. @rhettinger |
|
I prefer the zero-cost try/except to not slow down the loop. |
Ok wait to change |
Handle ValueError in random number generation loop.
please review this @rhettinger. |
Fix:
Replace _log2(random()) with _log2(1.0 - random()). This matches the standard algorithm (1.0 - random() is never zero), produces the correct distribution, and eliminates the crash.
binomialvariatefunction returns wrong results #149221