Weighted choices
Jason Friedman
jsf80238 at gmail.com
Sat Sep 7 22:12:34 EDT 2013
choices = dict()
choices["apple"] = 10
choices["pear"] = 20
choices["banana"] = 15
choices["orange"] = 25
choices["kiwi"] = 30
I want to pick sets of fruit, three in a set, where the chance of
selecting a given fruit is proportional to its weight. In the example
above, pears should appear twice as often as apples and kiwis should
appear twice as often as bananas.
I see this solution
(http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice)
but it is for single choices at a time. random.sample() does not
provide a "weight" argument.
I can picture how to write this with many, many lines of code but I
have a feeling there is an elegant solution.
More information about the Python-list
mailing list