[issue38881] unexpected behaviour of random.choices with zero weights
Mark Dickinson
report at bugs.python.org
Sat Nov 23 04:56:18 EST 2019
Mark Dickinson <dickinsm at gmail.com> added the comment:
Either raising, or treating a zero-weight-sum as undefined behaviour and documenting that the sum of the weights should be positive sounds fine to me.
-1 on the suggestion to (deliberately, by documented design) choose at random in this case. Mathematically, this situation doesn't make sense: as Tim said, it's analogous to choosing from an empty population.
Ex: you have `nred` red balls and `nblue` blue balls in a bag. If you want to simulate drawing a single ball from the bag, then
random.choices(["red", "blue"], [nred, nblue])
does the job. But in the case where `nred = nblue = 0`, the bag is empty and it's not possible to draw anything; in that case I'd expect an error. I definitely wouldn't expect to get either a red ball or a blue ball (with equal probability).
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38881>
_______________________________________
More information about the Python-bugs-list
mailing list