Force trial order reshuffle until a constraint is met

You’re right Michael! It was one of those situations where because more than 4 never occurred I assumed it was working!

This one seems to work fine and is a little less mind bending.
Oli

# -*- coding: utf-8 -*-
import random
"""
Brute Force PseudoRandom Ver 2
O.Clark
Since the list order needs to vary freely so it can repeat the same word list up to 
four times I have gone for a dictionary key approach.  It's not very sexy but it seems
to work (I can only eyeball so many outputs though - no black swans as of yet.

A list of indexes is generated before creating the trials and then converted into a string
so count can be used to see if any instance of 5 of the same trial in a row occur.  If they do - the list is reshuffled
(the brute force element) - if there are no instances of 5 of the same, a trial list is created by
iterating through the list of dictionary keys and popping the values within.
"""

wordList1 = [1,3,5,7,9,11,13,15,17,19,21] 
wordList2 = [2,4,6,8,10,12,14,16,18,20,22]

dictLists = {'l1':wordList1, 'l2':wordList2}
keyList = list(['l1','l2']*len(wordList1))

random.shuffle(wordList1)
random.shuffle(wordList2)

random.shuffle(keyList)
orderTest=False

while orderTest == False:
    for i in range(len(keyList)):
        if ''.join(keyList).count('l1'*5) > 0 or ''.join(keyList).count('l2'*5) >0:
            random.shuffle(keyList)
            break
        else:
            orderTest = True

trialList=[]

for thisKey in keyList:
    trialList.append(dictLists[thisKey].pop())

print trialList #this can be deleted, just for debugging purposes
1 Like