import random


def weighted_choice(items):
    """
    Chooses a random element from items, where items is a list of tuples in
    the form (item, weight). weight determines the probability of choosing its
    respective item.
    """
    weight_total = sum((item[1] for item in items))
    n = random.uniform(0, weight_total)
    # print(f"total: {weight_total} {n}")
    for item, weight in items:
        if weight > n:
            # print(f"{weight} {n}")
            return item
        n -= weight
    return items[-1][0]