Select k random elements from a list whose elements have weights
If the sampling is with replacement, you can use this algorithm (implemented here in Python): import random items = [(10, “low”), (100, “mid”), (890, “large”)] def weighted_sample(items, n): total = float(sum(w for w, v in items)) i = 0 w, v = items[0] while n: x = total * (1 – random.random() ** (1.0 / … Read more