import random


def weighted_choice(words):
    """Randomly chooses a word from a dict of words and frequencies

    Chooses a random number between 0 and the total frequency of all
    words given, and finds which bucket the number falls in

    Args:
        words: a dict of words paired with frequencies. For example:
        "one fish two fish red fish blue fish"
        will look like:
        {
            "one": 1,
            "fish": 4,
            "two": 1,
            "red": 1,
            "blue": 1
        }

    Returns:
        the randomly chosen string
    """
    number = random.random() * sum(words.values())
    for key, weight in words.items():
        number -= weight
        if number < 0:
            return key