Randomisation with constraints

Hello everyone,

I’m creating an experiment in which a set of sounds and images will be presented to participants, and we’ll record their responses using the keyboard.
Specifically, I have 48 different images and 24 different sounds repeated twice (attached a picture of my conditions).

I would like to randomize the order of the stimuli (sounds and images) making sure that the same sound is not repeated twice in a row.

I created a loop “RecallB1” and select Loop type to be ‘random’.

I’ve seen multiple topics trying to solve the same issue not online, like this one Randomisation without consecutive presentations - #2 by Michael

Can anyone please help me generating a code that will constrain the randomisation so that the same sound cannot be presented consecutively?

Thank you

Screenshot 2021-03-31 083630

The problem is that PsychoPy loops don’t support such constraints out of the box. However, you can write a little program that generates the right sequence for you and use that in a loop. This demo could serve as a starting point for this approach. Thomas Pronk / demo_dynamic_loops · GitLab

Hello Viviana

The solution provided by Michael works but you may not use a condition file as you did.

Best wishes Jens

HI @thomas_pronk
thank you, I had a look at the code, but am I right thinking that you selected stimuli based on the ‘name’ of the participant? (e.g. A- B- X)?

I’m not sure how I could use this as I don’t want to select a priori which stimuli will be presented

hi @JensBoelte

What do you mean by not using a condition file?

The picture of the excel file was just an example, in the experiment I’m presenting a multiple choice task in which participants need to select the correct picture pair between 4 possible options.
Therefore, my cond file looks like that:

Hello Viviana,

in the example you provide you specify an Excel-file “Recall_Block1.xlsx” as an conditions-file. This will not work with the code that Michael provides, at least that’s how I understand his comment: “The Builder loop can’t generate the sequence of correct responses with the constraint you need, which is why you now have custom code to do that (and to associate responses to stimulus positions). So don’t connect your loop to a conditions file. Instead, use the sequence that was generated in code.”

When you use a conditions-file, e.g. Recall_Block1.xlsx hexagon.png will always be presented with 2703.wav and 9622.wav. Isn’t that what you want?

You need to be more specific what you try to achieve, what is an example. Try to be as accurate as in a method section of a manuscript.

Best wishes Jens

@JensBoelte Sorry for not being clear. Let’s start from the beginning. (unfortunately, I cannot share the exp because of unpleasant stimuli).

LEARN 1: participants learn associations between sounds (24) and shapes (48).
Each sound is paired with 2 different shapes. (e.g. Trial1: sound A – shape 1 / Trial2: sound A – shape 2 …… Trial47: sound 24 – shape 47 / Trial48: sound 24 – shape 48).
MULTIPLEC_1: participants are tested on the learnt associations. I have 48 trials in which participants will hear a sound and have to choose the correct shape previously paired with it between 4 different shapes.


My excel file in the “MC1 loop” looks like that:


In each row I have one sound, 4 shapes and the correct answer.
the “MC1” loop is set to be random.
However, I want to avoid that participants will hear the same sound consecutively. Therefore, I would like something like that:


Trial 1: Green row
Trial 2 : yellow row
Trial 3 : blue row
Trial 4 : red row… etc

The experiment is perfectly working online with JS code, I would only like to make sure that when participants are tested on the learnt associations, the same sound will not be presented consecutively.

Yeah, the algorithm of Michael shows how to create a loop without repetitions, while my demo shows how your can customize a loop using certain logic. In my example that’s picking certain rows given a participant, but that can be adapted to the logic provided by Michael.

Hello Viviana

might need some testing, but the following code should mix randomly pictures with sounds and presents two different sounds to each picture (I’m using text-components instead of pictures and sounds):

Begin Experiment tab:

list_ok = False
double_stim = False
picture = " " 
soundfile = " " 
ldx = 0

index = [x for x in range(96)]

picList = [x for x in range(48)]*2
wavfileList = [x for x in range(24)]*4

while list_ok == False:
    for i in range(len(wavfileList) - 1):
        # check for equal neighbours:
        if wavfileList[i] == wavfileList[i + 1]:
            double_stim = True  # D'oh!
            break  # can stop checking on this run            
    if double_stim == True:
        shuffle(wavfileList)  # try a new order
        double_stim = False
        list_ok = True  # Yay! The loop won't run again.

Begin Routine tab

ldx = index.pop(0)
picture = picList[ldx]
soundfile = wavfileList[ldx]

End routine tab

thisExp.addData("picture", picture)
thisExp.addData("sound", soundfile)

picture and sound are two variables which I display via two text-components.

The trial is repeated with the help of a loop



You need some code to read in your stimuli. I used numbers to simplify it for me.

Best wishes Jens

1 Like

I run this code and it worked fine for the stimuli randomisation but it recorded just the last trial data ¿Could you help me to figure out what I did wrong, please?

Offline or online?

Offline…I just started to create my experiment (episodic generalization, Wearden & Bray, 2001) and need to present pairs of durations taken from two lists (four and seven durations), randomly. Thanks!

Offline this behaviour can occur if you untick “is trials” in your loops.

Ohh! Thanks! I will check it

Thank you very much! It worked!