Randomization of repeated trials

Hi there,

I’ve been struggling with the following problem for quite a while:

Say I have a condition file that contains 10 different stimuli. 4 of these stimuli have to be repeated 8 times and 6 of them have to be repeated 4 times, resulting in a total of 56 trials. So my condition file now has 56 rows that are repetitions of 10 different stimuli. Is there a smart way to randomize the trials so that no identical stimulus is presented directly after each other?

I do know that PsychoPy can handle the randomization in general, but as the file contains identical rows, I presume there is a good chance that two identical rows are selected after each other just by chance.

Thanks for any help or idea!


This is a common and simple question but not one with a simple answer.

PsychoPy’s TrialHandler can handle simple randomisation but not constraints like this: there are just too many that could be specified. Also the problem becomes non-deterministic. I’ve never found a better solution than this brute force approach:

  1. Shuffle the list.
  2. Cycle through it and see if it meets your constraints (e.g. no successive repeated values). If so, use it. If not, go back to (1).

Depending on your constraints, this might take dozens to hundreds or thousands of iterations to get a valid order of conditions. But in practice, it generally doesn’t take more than a few seconds to achieve.

Hi Michael,

I was thinking of a very similar brute force approach: shuffle the list manually by randomely selecting a row. If the row is the same as the previous one, put it back in the “pool” and select another one. Do you think that would take even more time?

Thanks for the reply :slight_smile: I will try out soon!


It’s certainly a valid approach but bear in mind that it is still not guaranteed to produce a solution in one iteration (e.g. what if the last two items are the same?) i.e. due to the random selection, it is still a brute-force approach rather than a deterministic algorithm that is guaranteed to produce a correct outcome. Your approach could indeed be slightly faster than the suggested route above but it all depends on the specific algorithm used.

Hey Michael,

I implemented the check for identical successive rows just as you suggested and everything works very fine! Thanks again for that :slight_smile: But now I have another problem:

This list contains trials for one block and needs to be repeated for 4 blocks in total. The idea was to shuffle the list and then use the sequential method of psychopy’s trial handle. But now I would always have the same random order for each repetition. Any smart idea how to deal with this situation?

Looking forward to your reply!

I mean a brute-forced solution would be to create 4 time the same list with different order and have 4 routines for the block instead of having one routine that is repeated 4 times… but I don’t really like this approach, because I would end up with a lot of excel files for example…

You should indeed run your shuffling algorithm four times, generating four separate conditions files. You run the same experimental task four times (i.e. run four TrialHandlers, using a sequential order as you note.

The only thing that changes is the name of the conditions file fed to the TrialHandler. i.e. you have a loop that cycles through a list of the four conditions file names. On each iteration of that loop, you create a new TrialHandler and add that to the ExperimentHandler. Then you iterate through the TrialHandler itself to run your actual trials.