Use random.shuffle()
and call it to randomise a list. Here is a generator that yields random values 'M' or "F' from a fixed pool in the relative amounts requested:
import random
def gender(n_male, n_female):
population = list('M' * n_male + 'F' * n_female)
random.shuffle(population)
for i in range(len(population)):
yield population[i]
>>> l = [g for g in gender(40, 60)]
>>> assert l.count('M') == 40
>>> assert l.count('F') == 60
>>> print(f"male : {l.count('M')}, female : {l.count('F')}")
male : 40, female : 60
>>> l = [g for g in gender(50, 50)]
>>> print(f"male : {l.count('M')}, female : {l.count('F')}")
male : 50, female : 50
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…