def drawIndex(probs, tolerant=False):
    """ Draws an index given an array of probabilities.

    :key tolerant: if set to True, the array is normalized to sum to 1.  """
    if not sum(probs) < 1.00001 or not sum(probs) > 0.99999:
        if tolerant:
            probs /= sum(probs)
        else:
            print((probs, 1 - sum(probs)))
            raise ValueError()
    r = random()
    s = 0
    for i, p in enumerate(probs):
        s += p
        if s > r:
            return i
    return choice(list(range(len(probs))))
dataset/ETHPy150Open pybrain/pybrain/pybrain/utilities.py/drawIndex/original