import random


def parent_idxs_choice(sorted_idxs, n_total, **optional_args):
    """Returns the idexes of the new parents for the next iteration. Used in Scheduler.loop()

    Args:
        sorted_idxs (list): Indexes of the sorted loss for the previous iteration.
        n_total (int): Length of the returned array.

    Returns:
        list: List containing the sorted indexes of the new parents for the next iteration.
    """
    acceptance_probability = optional_args.get("accept_prob", 0.9)
    new_idxs = [{}] * n_total

    i = 0
    j = 0
    while i < n_total and j < len(sorted_idxs) - (n_total - i):
        if random.uniform(0, 1) < acceptance_probability:
            new_idxs[i] = sorted_idxs[j]
            i += 1
        j += 1

    while i < n_total:
        new_idxs[i] = sorted_idxs[j]
        i += 1
        j += 1

    return new_idxs