random.choices() Suggest that the code confirm that cum_weights sequence is in ascending order

Paul tallpaul at gmail.com
Sun May 13 20:48:47 EDT 2018


Hi,
  I just learned how to use random.choices().  I initially misunderstood
the documentation for cum_weights as meaning that a cumulative sequence
would be *constructed from* the sequence which I supplied.  Consequently, I
specified 'cum_weights' with a sequence which wasn't in ascending order.  I
got back k results but I determined that they weren't correct (eg, certain
population values were never returned).

  Since the non-ascending sequence, which I had supplied, could not
possibly be valid input, why isn't this checked (and an error returned)?
 Returning incorrect results (which could be hard to spot as being
incorrect) is much more dangerous.  Also, checking that the list is in
ascending order need only be done once, and seems like it would be
inexpensive.

  Thanks,
  Paul



More information about the Python-list mailing list