I\'m a bit at a loss as to how to find a clean algorithm for doing the following:
Suppose I have a dict k:
>>> k = {\'A\': 68, \'B\': 62, \'
This should do the trick:
>>> k = {'A': 68, 'B': 62, 'C': 47, 'D': 16, 'E': 81}
>>> import random
>>> def weighted_pick(dic):
... total = sum(dic.itervalues())
... pick = random.randint(0, total-1)
... tmp = 0
... for key, weight in dic.iteritems():
... tmp += weight
... if pick < tmp:
... return key