dimanche 29 mars 2015

Generating random numbers in range until a max for that number has been met

I'm trying to do something basic - use range 0-4 and generate a 36 number sequence such that each value is generated 9 times.


The var trial_init is just an array of 36 zeros.


I have generated:



trial_seq = []

for i in range(len(trial_init)-1):
while True:
trial_val = random.choice(range(4))
if (trial_seq.count(0) <=9 and trial_seq.count(1) <=9 and trial_seq.count(2) <=9 and trial_seq.count(3) <=9):
break
trial_seq.append(trial_val)


I guess I'm not sure exactly why it doesn't work.


It should generate a number 0, 1, 2 or 3 and to the extent that number has not been included in the trial_seq array more than 9 times, it should add it to the trial_seq array, otherwise it should generate another number, until 36 numbers have been generated.





Aucun commentaire:

Enregistrer un commentaire