A weighted version of random.choice
Since version 1.7.0, NumPy has a
choice function that supports probability distributions.
from numpy.random import choicedraw = choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution)
probability_distribution is a sequence in the same order of
list_of_candidates. You can also use the keyword
replace=False to change the behavior so that drawn items are not replaced.
In : import randomIn : random.choices(...: population=[['a','b'], ['b','a'], ['c','b']],...: weights=[0.2, 0.2, 0.6],...: k=10...: )Out:[['c', 'b'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['c', 'b']]
random.choices will sample with replacement, per the docs:
ksized list of elements chosen from the population with replacement.
Note for completeness of answer:
When a sampling unit is drawn from a finite population and is returnedto that population, after its characteristic(s) have been recorded,before the next unit is drawn, the sampling is said to be "withreplacement". It basically means each element may be chosen more thanonce.