| Reference | Downloads | Github

Change the element that is randomly chosen at each trial

Hi all,
I’m creating a face XAB task, such that a face X appears and then the participant has to decide which of two faces (A or B) is the same face as face X shown a few seconds earlier. I have a conditions file with face images. I have the loop type set to “random” for each trial, as the faces are sorted by gender but I want them to be randomly mixed up. However, I don’t want any of the faces from one trial to show up in a consecutive trial. I’ve made a list of “avoidFaces” that updates at each trial, and I am able to check if face X (which is randomly chosen from my conditions file) is in avoidFaces or not. I want to be able to say that “if face X is in avoidFaces, then pick a new random face”. Is there a way to regenerate the randomly chosen face from my conditions file that is selected through PsychoPy?

Thank you!

I would recommend loading the images into an array before you start your trials. Then you can step through that array at your own place, rather than once per trial.

You can see how I’ve done this in where I load targets and fillers and then step through trials either taking a word from the filler list or a word from the target list.

Best wishes,


1 Like

Elaborating on what wakecarter said, this piece of code repeats creating a random sequence until the condition is satisfied that stimuli never repeat directly. Hope this helps.

stims = ['A', 'B', 'C', 'D'] #example list of stimuli
all_stims = stims*4          #repeating the stimuli

import random

success = 0
while success == 0:
    for index in range(len(all_stims)-1):        #loop through the list (but not the last element)
        if all_stims[index]==all_stims[index+1]: #if the element is the same as its successor
            random.shuffle(all_stims)            #randomize 
            success = 0                          #set flag to 0
            break                                #start looping again
            success = 1                          #set flag to 1 and proceed with next element

Thank you both. These suggestions were very helpful.