for k, w in d.items():
    s += w
if r < s:
    return k
return k

def Test():
    k = {'A': 68, 'B': 62, 'C': 47, 'D': 16, 'E': 81}
results = {}
for x in range(10000):
    p = WeightedPick(k)
results[p] = results.get(p, 0) + 1
print(results)
Test()
(Python) algorithm to randomly select a key based on proportionality/weight