Reputation: 25
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.
Upvotes: 0
Views: 126
Reputation: 36472
I assume this is python.
You could actually go the other way around: generate an index set, and randomly assign indices from that to each of the four possible values:
trial_seq = [0]*36
indices = range(36)
random.shuffle(indices)
for i in range(36):
trial_seq[indices[i]] = i % 4;
EDIT: Oh man, at times I'm stupid:
trial_seq = [ i % 4 for i in xrange(36) ]
random.shuffle(trial_seq)
does the same.
Upvotes: 1