import random


def weighted_random_choice(list_of_possible):
    """Choose one of the candidates at random weighted by their (avg) bandwidth
    :params list_of_possible: list of descriptors of the candidates
    :returns: descriptor of the randomly selected router"""

    total = 0
    for router in list_of_possible:
        total += router['bandwidth']['avg']

    r = random.uniform(0, total)
    upto = 0

    for router in list_of_possible:
        if upto + router['bandwidth']['avg'] >= r:
            return router
        upto += router['bandwidth']['avg']

    raise RuntimeError("")