import random


def choose_promo(promo_list):
    """
    This is the algorithm to pick which promo to show.

    This takes into account how many remaining days this
    promo has to be shown.

    The algorithm is currently as such:

    * Take the remaining number of views each promo has today
    * Add them together, with each promo "assigned" to a range
    * Pick a random number between 1 and that total
    * Choose the ad whose range is in the chosen random number

    In the future,
    we should take into account the expected views for today
    (The number of views from this day last week)
    Then we can scale the "total ads sold" against that "expected views",
    and that will give us more spread throughout the day.

    """
    promo_range = []
    total_views_needed = 0
    for promo in promo_list:
        promo_range.append([
            total_views_needed,
            total_views_needed + promo.views_needed_today(),
            promo
        ])
        total_views_needed += promo.views_needed_today()
    choice = random.randint(0, total_views_needed)
    for range_list in promo_range:
        if range_list[0] <= choice <= range_list[1]:
            return range_list[2]
    return None