I donβt understand a bit how to find a clean algorithm to do the following:
Suppose I have dict k:
>>> k = {'A': 68, 'B': 62, 'C': 47, 'D': 16, 'E': 81}
Now I want to randomly select one of these keys based on the "weight" that they have in the sum (i.e. the sum) of the keys.
>>> sum(k.values()) >>> 274
So there
>>> 68.0/274.0 >>> 0.24817518248175183
24.81% percentage change, selected A.
How do you write an algorithm that takes care of this? In other words, does it guarantee that with 10,000 random samples A will be selected 2.481 times?
python
LaundroMat Apr 03 '10 at 8:41 2010-04-03 08:41
source share