import random


def random_select(p_list):
    """
    Randomly select an index from an iterable list of probabilities
    Inputs:
        p_list, a list of probabilities that sums to 1
    Outputs:
        An index from p_list
    """
    target_num = random.uniform(0,1)
    cum_prob = 0
    
    #Add up probability until the first index that passes cum_prob
    # and return that index, if the probabilities do not sum to one, and the
    # target number is greater, we simply return the last index
    for i in range(0,len(p_list)):
        cum_prob += p_list[i]
        if cum_prob >= target_num:
            return i
    return len(p_list)-1