import random


def roll_unweighted_die(weights=None):
    """Returns the result of an unweighted die.

    This uses a fair

    Args:
        weights: (integer array) a collection of the percentage chances each
                 result of the die has. The number of sides is determined by
                 the number of provided weights, and the sum of the weights
                 must be 1.0 (100%)

    Raises:
        ValueError: Provided weights did not add up to 1 (100%)
    """
    if weights is None or sum(weights) != 1:
        raise ValueError('Weight (%s) do not add to 1.0' % (weights))

    # Roll the die
    fair_roll = random.random()

    # Which range did the selected value fall into?
    res = 0
    for weight in weights:
        res = res + 1
        if fair_roll <= weight:
            return res

        # If weights are [0.3, 0.2, 0.5], and 0.45 is rolled, removing the
        # weight from the roll allows us to compare it to that section the
        # range, compared to the previous possible values
        fair_roll -= weight

    # This can happen because of floating point inaccuracies. Just use the
    # last value if we leave the loop
    return res