import random


def weighted_sampler(pop_dict):
    """randomly sample a dictionary's keys based on weights stored as values example:
           m = {'a':3, 'b':2, 'c':5}
           samps = [weighted_sampler(m) for _ in range(1000)]
           #samps should be a ~ 300, b ~ 200, and c ~ 500
           >>> samps.count('a')
           304
           >>> samps.count('b')
           211
           >>> samps.count('c')
           485
       of course, being a random sampler your results will vary"""
    ch = random.random() * sum(pop_dict.values())
    f = sorted(pop_dict.keys())
    for i, w in enumerate([pop_dict[x] for x in f]):
        ch -= w
        if ch < 0: return f[i]