I have a program where I\'m keeping track of the success of various things using collections.Counter
— each success of a thing increments the corr
Another variant, Setup is a bit cumbersome, but lookup is in logarithmic complexity (suitable when several lookups are needed):
import itertools
import random
from collections import Counter
from bisect import bisect
counter = Counter({"a": 5, "b": 1, "c": 1})
#setup
most_common = counter.most_common()
accumulated = list(itertools.accumulate([x[1] for x in most_common])) # i.e. [5, 6, 7]
total_size = accumulated[-1]
# lookup
i = random.randrange(total_size)
print(most_common[bisect(accumulated, i)])