def weighted_choice(item_probabilities):
    """ Randomly choses an item according to defined weights
    Args:
        item_probabilities: list of (item, probability)-tuples
    Returns:
        random item according to the given weights
    """
    probability_sum = sum(x[1] for x in item_probabilities)
    assert probability_sum > 0
    random_value = random.random() * probability_sum
    summed_probability = 0
    for item, value in item_probabilities:
        summed_probability += value
        if summed_probability > random_value:
            return item
Randomly choses an item according to defined weights
    Args:
        item_probabilities: list of (item, probability)-tuples
    Returns:
        random item according to the given weights