I need to generate a fixed-length string with only 'y' and 'n' in a random order, and the number of 'n' in the string is determined by the variable Njk. The strings must be like 'yyyyynynynyyyyyy' or 'yynyyyynyyynyyyy', and I'll generate a huge amount of strings. Currently I'm doing like the code below. The problem is, to generate 200 strings of length 16, it works fine, but when I try to generate 200 strings of length 33, it takes so much time to execute that is making my work unviable. How can I do it in a optimal/efficient way?
Best regards!
#chaves = 16
Njk = 3
#Generating a random key state
def rand_estado():
aux2 = randint(0,1)
if aux2 == 0:
estado = 'n'
if aux2 == 1:
estado = 'y'
return estado
#Generating a random combination of key states with fixed number of 'y' and 'n'
def rand_estado_chaves():
radialidade = 0
#The condition that must be satisfied
while (radialidade != chaves - Njk):
estado_chaves = ""
#For each key
for i in range(chaves):
estado_chaves += rand_estado()
radialidade = estado_chaves.count('y')
return estado_chaves