Fix IndexError in choices_distribution when uniform() returns 1.0#2328
Open
bysiber wants to merge 1 commit intojoke2k:masterfrom
Open
Fix IndexError in choices_distribution when uniform() returns 1.0#2328bysiber wants to merge 1 commit intojoke2k:masterfrom
bysiber wants to merge 1 commit intojoke2k:masterfrom
Conversation
random.uniform(0, 1.0) can return exactly 1.0, causing bisect_right to return len(cdf2) which is one past the last valid index. Clamp the index to avoid the overflow.
Collaborator
|
Thank you! Could you add a unit test? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
choices_distribution_unique(and the fallback path ofchoices_distribution) can crash withIndexErrorwhenrandom.uniform(0, 1.0)returns exactly1.0.The Bug
random.uniform(0, 1.0)can return1.0— from the Python docs: "The end-point value b may or may not be included" and the implementation note saysa + (b-a) * random()can returnb.When this happens,
bisect.bisect_right(cdf2, 1.0)returnslen(cdf2)because the last CDF element is always1.0andbisect_rightplaces equal values to the right. This index is one past the end:This is intermittent in production —
uniform()returning exactly1.0is rare but will happen given enough calls.The Fix
Clamp the index to the valid range:
Applied to both
choices_distribution_uniqueand the fallback path ofchoices_distribution.