-
-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
random.choices has unexpected behavior with negative weights #81805
Comments
The behavior of random.choices when negative weights are provided is unexpected. While giving a negative weight for one value is probably bad, it's really unfortunate that providing a negative weight for one value affects the probability of selecting an adjacent value. Throwing a ValueError exception when there was a use of negative weights was considered in bpo-31689, but at that time, there wasn't an example provided that failed silently. Note below that providing a weight of -1 for 'c' causes both 'c' and 'd' to drop out of the results. Python 3.7.2 (default, Jan 13 2019, 12:50:01)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>> from random import choices
>>> Counter(choices("abcdefg", weights=(1,1,-1,1,1,1,1), k=10000))
Counter({'f': 2040, 'a': 2019, 'e': 2017, 'g': 2009, 'b': 1915}) |
This is what happens with your weights: >>> list(itertools.accumulate(weights))
[1, 2, 1, 2, 3, 3, 4] using bisect.bisect certain amount of times, will distribute on this: random numbers between 1 and 2 will go to 1<'b'<2 and not 'd' that is also 1<'d'<2 As discussed in bpo-31689, if no negative weights should be used, then a ValueError exception should be re-concidered. |
We can add a note the the docs that weights are assumed to be non-negative, but I don't want to add an extra O(n) step to check for unusual inputs with undefined meaning -- that would just impair the normal use cases for near zero benefit. |
Misc side notes:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: