Message348128
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 #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}) |
|
Date |
User |
Action |
Args |
2019-07-18 20:51:27 | Ted Whalen | set | recipients:
+ Ted Whalen |
2019-07-18 20:51:27 | Ted Whalen | set | messageid: <1563483087.67.0.502160957711.issue37624@roundup.psfhosted.org> |
2019-07-18 20:51:27 | Ted Whalen | link | issue37624 messages |
2019-07-18 20:51:27 | Ted Whalen | create | |
|