New submission from Ted Whalen <tewha...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue37624>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to