Randomization without double or triple repeats

I have an experiment where a random number (1,2, or 3) is chosen at the beginning of the experiment and determines which CSV file is used during the loop. The code I am using:

Begin Experiment

rand = [1,2,3]*4
shuffle(rand)

I put this in the begin experiment tab to ensure that each number is chosen exactly 4 times without replacement.

This code works; however, it often happens that a number is chosen 2 times in a row, or even 3 times in a row. For this reason, I am looking to adjust the code so that a number cannot be chosen 3 times in a row, or 2 times in a row (ex: generating a list of 1,2, and 3) 4 different times rather than one list of 12 values

Have a look at a couple of my online demos in PsychoPy Online Demos

Randomisation without repetition and Complex Randomisation may give you some pointers.

Hello,
Here is another idea on how to accomplish it:

rand = [1,2,3]
randomList = []

for i in range(4):
    shuffle(rand)
    randomList = [*randomList, *rand]

# [1, 2, 3, 3, 2, 1, 2, 3, 1, 3, 2, 1]
# [2, 3, 1, 1, 3, 2, 3, 2, 1, 2, 3, 1]
# [1, 2, 3, 1, 2, 3, 2, 3, 1, 2, 1, 3]
# [3, 2, 1, 3, 2, 1, 2, 3, 1, 3, 2, 1]
# [2, 1, 3, 2, 1, 3, 3, 2, 1, 3, 2, 1]

Chen

1 Like