I am confused using the new random.choices
in Python 3.6.
Here is the doc :
random.choices(population, weights=None, *, cum_weights=None, k=1)
-- Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises
IndexError
.
They give an example: weights=[10, 5, 30, 5]
and I don't know what this means. Why don't they sum to 100? If my population is [1, 2, 3, 4]
-- does this mean that a choice of '10' occurs with probability 0.1?
The total weight is 10+5+30+5=50.
Suppose the population is [1,2,3,4].
It returns 1 with probability 10/50 = 0.2
It returns 2 with probability 5/50 = 0.1
It returns 3 with probability 30/50 = 0.6
It returns 4 with probability 5/50 = 0.1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.