Randomize trial order without repeating stimuli twice in a row

I don’t use Builder, but I have a piece of code (using numpy) at the start of an experiment that loops through my randomised list of stimuli to check for identical items in subsequent rows, and reshuffles the list (and checks again, and reshuffles if needed…) until there are none. This does introduce a short waiting time at the start, but never too long in my experience.

# make sure the same target doesn't appear on consecutive trials
doubletarget = False
listok = False

while listok == False:
    for i in range(len(practiselist)-1):
        if practiselist[i,stimTextCol] == practiselist[i+1,stimTextCol]:
            doubletarget = True
    if doubletarget == True:
        practiselist = np.random.permutation(practiselist)
        print("reshuffle practiselist...")
        doubletarget = False
    else:
        listok = True
2 Likes