def _select(state, rank_number):
    """
    rank_number (index):  An index into the archive.

    Returns a GridPoint from the given rank.
    If the rank is empty, raises an Exception.
    If the rank number is outside the archive, raises
    an IndexError.  Don't do that!
    """
    rank = state.archive[rank_number]
    if rank.occupancy == 0:
        raise Exception("Can't select from an empty rank ({}).".format(
            rank_number))
    randint = state.randint
    target = randint(0, rank.occupancy - 1)
    counter = -1
    index = -1
    while counter < target:
        index += 1
        if rank.individuals[index].valid:
            counter += 1
        if index > len(rank.individuals):
            raise Exception('Rank {} is inconsistent!'.format(rank_number))
    return rank.individuals[index].grid_point
