Randomization of Image Presentation

Dear Forum,
I want to randomise the positions of images on the screen in a grid of 9 potential locations.

The 9 images belong to 3 categories (A,B,C)
ex. 3 different Pasta images (A1,A2,A3) , 3 different fruit images, 3 different landscapes

I need to randomise 2 things:

  1. The position of the images according to their category these 3 images should end up in the same row of the grid

  2. Within one category the location of the images needs to be randomised in that row
    ex. within fruit the position of strawberry needs to be randomised between right middle left in the previously selected row

the 9 images are only presented once to the participant. Each trial has 9 different images.

I am really grateful for any help on how to approach this since I am completely new to programming.
Thank you in advance!


should each image appear on each position in the run of the experiment?

Best wishes Jens

Hello Jens,

Each image is presented only once per participant. Each out of 80 trials has 9 new images
The image location should be randomised between participants.



sorry, I still don’t get it. There are 9 images which you want to show only once, right? Given that there are 9 positions, this is one trial, right. The other 80 trials do not matter?

Best wishes Jens

I am sorry for not being clear. Exactly what you said. The other 80 trials do not matter since the images and categories are new in every trial.

I hope I understood your question correctly

Hello Leonie,

ok, create a code-component with three x and y-positions for each image you want to show and initialize it:

pasta1x = 0
pasta2x = 0
pasta3x = 0
pasta1y = 0
pasta2y = 0
pasta3y = 0
fruit1x = 0
fruit2x = 0
fruit3x = 0
fruit1y = 0
fruit2y = 0
fruit3y = 0
land1x = 0
land2x = 0
land3x = 0
land1y = 0
land2y = 0
land3y = 0

xpos = [-.3, 0, .3]
ypos = [-.25, 0, .25]

This goes in the Begin Experiment tab of the component. In the Begin Routine tab add the following code:

pasta1y = ypos[0]
pasta2y = ypos[0]
pasta3y = ypos[0]
fruit1y = ypos[1]
fruit2y = ypos[1]
fruit3y = ypos[1]
land1y = ypos[2]
land2y = ypos[2]
land3y = ypos[2]

pasta1x = xpos[0]
pasta2x = xpos[1]
pasta3x = xpos[2]

fruit1x = xpos[0]
fruit2x = xpos[1]
fruit3x = xpos[2]

land1x = xpos[0]
land2x = xpos[1]
land3x = xpos[2]

I shuffle the x-position three times. This prevent clustering of positions across categories, all images1 are on position 0, all images2 are on position 2 aso…

If you want to save the positions, add the following code to the End routine tab:

thisExp.addData("fruit1x", fruit1x)
thisExp.addData("fruit1y", fruit1y)
thisExp.addData("fruit2x", fruit2x)
thisExp.addData("fruit2x", fruit2y)
thisExp.addData("fruit3x", fruit3x)
thisExp.addData("fruit3y", fruit3y)
thisExp.addData("pasta1x", pasta1x)
thisExp.addData("pasta1y", pasta1y)
thisExp.addData("pasta2x", pasta2x)
thisExp.addData("pasta2y", pasta2y)
thisExp.addData("pasta3x", pasta3x)
thisExp.addData("pasta3x", pasta3y)
thisExp.addData("land1x", land1x)
thisExp.addData("land1y", land1y)
thisExp.addData("land2x", land2x)
thisExp.addData("land2y", land2y)
thisExp.addData("land3x", land3x)
thisExp.addData("land3y", land3y)

Now, create nine image-components (fruit1, fruit2, fruit2, pasta1 aso.), one for each image. Change the location property of each image-component (e.g. fruit1; pasta1) to get a unique position (e.g. fruit1x, fruit1y; pasta1x, pasta1y respectively) and set it to set every repeat.


Best wishes Jens


Thank you so much this is extremely helpful!

Dear Jens, it works perfectly. I just have a short follow up question.

I also need to randomise between 2 other conditions.
A) Either one row is one category (like your code)
B) One column is one category

I am using an extended version of your code at the moment to achieve this.
What I am missing is: How can I ensure a balanced number between these 2 conditions
(50% of trials for A, 50% for B) ?

if random.random()>0.5:   
    A1y = ypos[0]
    A2y = ypos[0]
    A3y = ypos[0]
    B1y = ypos[1]
    B2y = ypos[1]
    B3y = ypos[1]
    C1y = ypos[2]
    C2y = ypos[2]
    C3y = ypos[2]
    A1x = xpos[0]
    A2x = xpos[1]
    A3x = xpos[2]

    B1x = xpos[0]
    B2x = xpos[1]
    B3x = xpos[2]

    C1x = xpos[0]
    C2x = xpos[1]
    C3x = xpos[2]
    A1x = xpos[0]
    A2x = xpos[0]
    A3x = xpos[0]
    B1x = xpos[1]
    B2x = xpos[1]
    B3x = xpos[1]
    C1x = xpos[2]
    C2x = xpos[2]
    C3x = xpos[2]

    A1y = ypos[0]
    A2y = ypos[1]
    A3y = ypos[2]

    B1y = ypos[0]
    B2y = ypos[1]
    B3y = ypos[2]

    C1y = ypos[0]
    C2y = ypos[1]
    C3y = ypos[2]        

# update component parameters for each repeat
B2.setPos((B2x, B2y))
B1.setPos((B1x, B1y))
B3.setPos((B3x, B3y))
A1.setPos((A1x, A1y))
A2.setPos((A2x, A2y))
A3.setPos((A3x, A3y))
C1.setPos((C1x, C1y))
C2.setPos((C2x, C2y))
C3.setPos((C3x, C3y))

Hello Leonie,

that is within one participant?

Best wishes Jens

yes that is within one participant

Hello Leonie,

within one block or across two blocks (one column-wise, one row-wise)? Sorry, I need more details.

Best wishes Jens

I will most likely have 4 blocks with 20 trials each.
Within each block the amount of column-wise trials and row-wise trials should be equal.

Thank you for taking the time!

Hello Leonie,

in the Begin routine tab of the relevant block add the following:

ranList = [0,1]*10

if ranList.pop():
    #add here the code for column-wise categories
    #add here the code for row-wise categories

Notice that ranList has twenty elements. It will crash if you have more trials per block.

Best wishes Jens
BTW: it best to open a new topic when a new problem comes up.