[New-bugs-announce] [issue38881] unexpected behaviour of random.choices with zero weights

Iza Romanowska report at bugs.python.org
Thu Nov 21 12:35:22 EST 2019


New submission from Iza Romanowska <izaromanowska at gmail.com>:

Hi, 
When zero weights are given, the last element of a sequence is always chosen. Example: 

hits= []
for i in range(100):
    hits.append(random.choices(["A","B","C","D"], [0, 0, 0, 0])[0])
print (set(hits))

>> {'D'}

I guess that most users would expect that in case of zero weights it will default into a random.choice behaviour and select one option at random since this is what happens in cases when all weights are equal. Alternatively, it should return an empty array if the assumption was that all choices have a zero probability of being selected. Either way, if it is consistently choosing one option, this may be potentially difficult to spot in situations when a sequence of weights all equal to zero only happen sporadically.

----------
components: Library (Lib)
messages: 357185
nosy: IRomanowska
priority: normal
severity: normal
status: open
title: unexpected behaviour of random.choices with zero weights
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38881>
_______________________________________


More information about the New-bugs-announce mailing list