mercredi 21 juillet 2021

Why does "random.choices" return always the same element when passing a list "cum_weights" of decreasing values?

I don't understand the cum_weights parameter of random.choices.

I read that it is:

Weight of previous element + own weight [10, 5, 1] → [10, 15, 16]

So as I understand it, the probability of "cherry" is 16 and it is the highest. So why is "apple" more repetitive as a result?

import random

mylist = ["apple", "banana", "cherry"]
print(random.choices(mylist, cum_weights=[10, 5, 1], k=9))

outputs:

['apple', 'apple', 'apple', 'apple', 'apple', 'apple', 'apple', 'apple', 'apple']



Aucun commentaire:

Enregistrer un commentaire