[New-bugs-announce] [issue41773] Clarify handling of infinity and nan in random.choices

Ram Rachum report at bugs.python.org
Sat Sep 12 11:31:17 EDT 2020


New submission from Ram Rachum <ram at rachum.com>:

I was recently tripped up by a bug caused by passing infinite weights to random.choices. I toyed around with that function, and it seemed that when it's given weights that include infinity or NaN, it selects a specific element, always without being random. That's regardless of whether multiple items have infinity weights. It chooses a different specific item if the infinity is negative. The specific item isn't always the one that has the infinite weight.

I don't know whether that's the intended behavior for some reason, or whether it's even possible to define a logical behavior for infinite weights. If it's not possible, then maybe this function should raise an errors when given weights that include infinity or nan.

I see that the documentation states that the weights must be non-negative; maybe we should have a check for that too.

----------
components: Library (Lib)
messages: 376802
nosy: cool-RR
priority: normal
severity: normal
status: open
title: Clarify handling of infinity and nan in random.choices
type: behavior
versions: Python 3.10

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


More information about the New-bugs-announce mailing list