[New-bugs-announce] [issue37624] random.choices has unexpected behavior with negative weights
Ted Whalen
report at bugs.python.org
Thu Jul 18 16:51:27 EDT 2019
New submission from Ted Whalen <tewhalen at gmail.com>:
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})
----------
components: Library (Lib)
messages: 348128
nosy: Ted Whalen
priority: normal
severity: normal
status: open
title: random.choices has unexpected behavior with negative weights
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37624>
_______________________________________
More information about the New-bugs-announce
mailing list