import random


def choose_wyckoff(group, number):
    """
    Choose a Wyckoff position to fill based on the current number of atoms
    needed to be placed within a unit cell
    Rules:
        1) The new position's multiplicity is equal/less than (number).
        2) We prefer positions with large multiplicity.

    Args:
        group: a pyxtal.symmetry.Group object
        number: the number of atoms still needed in the unit cell

    Returns:
        a single index for the Wyckoff position. If no position is found,
        returns False
    """
    wyckoffs_organized = group.wyckoffs_organized
    
    if random.uniform(0,1)>0.5: #choose from high to low
        for wyckoff in wyckoffs_organized:
            if len(wyckoff[0]) <= number:
                return random.choice(wyckoff)
        return False
    else:
        good_wyckoff = []
        for wyckoff in wyckoffs_organized:
            if len(wyckoff[0]) <= number:
                for w in wyckoff:
                    good_wyckoff.append(w)
        if len(good_wyckoff) > 0:
            return random.choice(good_wyckoff)
        else:
            return False