Randomise stimuli list without surrounding stimuli appearing consecutively

Hi Paige,

Here is a suggestion that adapts @dvbridges solution to a related problem. The suggestion assumes that you have a list of numbers that you randomise.

import random

while 1:
    choices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    shuffledList = []

    previous = ""

    while choices:

        tmpList = choices
        
        # if we've already found one element
        if previous:
            #print(previous)
            # find all list elements that are not direct neighbours of the previous element
            tmpList = [x for x in tmpList if x != previous-1 and x != previous+1]
            #print(tmpList)
        
        # there are no elements left that are not direct neighbours
        if not tmpList:
            print("\nno valid solution")
            break 
        
        # choose a random element from the reduced list
        newEl = random.choice(tmpList)
        
        # append this element to the new list
        shuffledList.append(newEl)
        
        # remove the chosen element from *choices* (not tmpList!)
        choices.remove(newEl)
        
        # remember the element for the next iteration
        previous = newEl
        
    # if choices is empty (i.e., all elements were successfully moved to shuffledList)
    if not choices:
        print(shuffledList)
        break

To apply the result to your Excel input file, you could follow these instructions. Note that you would then need to choose sequential as loopType. If you wanted to, you could create a separate input file for each participant.

Hope this helps.

Jan