# How to randomise pairs of images

**URL:** <https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966>\
**Category:** Builder\
**Created:** [November 15, 2022, 4:02pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966 "2022-11-15T16:02:20Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 15, 2022, 4:02pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/1 "2022-11-15T16:02:20Z")

</div>

Hello everyone,

I’m starting to build a pairwise comparison task with image. Two image appear in the same time on the screen (one at the left and one at the right).  
My problem is : when I’m running the experiment the two image are the same cause in the Excel file there is the same image paired.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/5/c569cffa2718b444df2dbc3db2758f436a49a216.png)

I want it to be random and know in my data file which images were presented for each trial.

Is there any manner to make them appear randomly left or right for every test subject?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/7/17db23a291d156115a5c0ffc66a2b5af163286cc.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/3/83ba0fa6152f32b992a7a0633b3ac83288306910.png)

Thanks for your help,

Milèna

---

<div class="post-metadata">

**Author:** ![bwinston](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/bwinston/32/14629_2.png) [@bwinston](https://discourse.psychopy.org/u/bwinston)\
**Post date:** [November 15, 2022, 4:26pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/2 "2022-11-15T16:26:09Z")

</div>

hi @Milena this might not be the best way to do it, but…

you could write Python code to generate an excel file for each subject as the experiment is starting that randomizes the order of each column.

you could have the master file above live in your experiment directory, then create a per-subject file on the fly when the experiment is run. then in the ‘Conditions’ field, you could have a variable referring to the per-subject file.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 15, 2022, 5:05pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/3 "2022-11-15T17:05:05Z")

</div>

What I do in this circumstance is have the columns fixed and shuffle a list of coordinates (or x positions) each trial. Make sure you save the x position of the target

---

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 16, 2022, 8:27am UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/4 "2022-11-16T08:27:02Z")

</div>

Hi,  
I can’t code because I don’t know how to do that…  
But it’s complicate Python code ?

Thanks

---

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 16, 2022, 8:29am UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/5 "2022-11-16T08:29:40Z")

</div>

Should I have one column with my images and one column with positions?

Thanks

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 16, 2022, 9:23am UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/6 "2022-11-16T09:23:41Z")

</div>

Add a code component to your trial routine:

Begin Experiment

```auto
locations = [[.3,0],[-,3,0]]

```

Begin Routine

```auto
shuffle(locations)
thisExp.addData(''Location1",locations[0])

```

Then add locations[0] as the position for one image and locations[1] as the position for the other, to be updated each routine.

If your task is to say which food has more calories, then you could put the higher calorie food in aliment1 and then add code in Begin Routine for to calculate the correct answer:

```auto
if locations[0][0] > 0:
     Answer = 'right'
else:
     Answer = 'left'

```

For online use, you’d need to add Answer = ‘’ in Begin Experiment

---

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 16, 2022, 12:54pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/7 "2022-11-16T12:54:54Z")

</div>

I have change

```auto
locations = [[.3,0],[-,3,0]]
by 
locations = [[.3,0],[-.3,0]]

```

But I have a problem with this code

```auto
shuffle(locations)
thisExp.addData(''Location1",locations[0])

```

I try to put :

```auto
shuffle(locations)
thisExp.addData('locations[1]','locations[0]')

```

But it doesn’t work  
When I run my experiment with the code I have this error message :

3.0738 WARNING User requested fullscreen with size [1024 768], but screen is actually [1920, 1080]. Using actual size  
Traceback (most recent call last):  
File “C:\Users\Milena\Desktop\EXP\untitled\_lastrun.py”, line 201, in   
shuffle(locations)  
File “mtrand.pyx”, line 4417, in numpy.random.mtrand.RandomState.shuffle  
TypeError: object of type ‘int’ has no len()

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 16, 2022, 1:08pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/8 "2022-11-16T13:08:06Z")

</div>

> [@Milena](#):
>
> ```auto
> shuffle(locations)
> thisExp.addData(''Location1",locations[0])
> 
> ```

I made a mistake in my code (I was typing on a mobile). It should have been

```auto
shuffle(locations)
thisExp.addData('Location1',locations[0])

```

This code is about saving the location of the first column

The first word (in quotes) is the name of the column in the data file  
The second word (no quotes) is the variable to be saved. locations[0][0] should just save the x coordinate

If you want to also save the x coordinate of the second location, it should be

```auto
thisExp.addData('Location2',locations[1][0])

```

---

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 17, 2022, 12:26pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/9 "2022-11-17T12:26:29Z")

</div>

I don’t have any more bugs with the code thanks.  
But it still shows me the same images. Should I add a collone in my excel file?

Thanks

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 17, 2022, 12:33pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/10 "2022-11-17T12:33:07Z")

</div>

At the moment you are seeing the same image on both sides because you have the same image twice in each row of your Excel file.

Would you prefer to  
a. Have each image paired with a specific other image, but randomise which one is on the left or  
b. Randomly pair images from your image set?

---

<div class="post-metadata">

**Author:** ![Milena](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Milena](https://discourse.psychopy.org/u/Milena)\
**Post date:** [November 17, 2022, 12:48pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/11 "2022-11-17T12:48:09Z")

</div>

I prefer the b

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 17, 2022, 1:28pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/12 "2022-11-17T13:28:46Z")

</div>

Based on the Multi-session randomisation demo I wrote this morning, I’d just written a “low code” demo for Random Pairs

# Random Pairs [code](https://gitlab.pavlovia.org/vespr/random-pairs) | [try it](https://run.pavlovia.org/vespr/random-pairs)

In this demo there is an outer loop that cycles through all stimuli. However, the trial itself is only presented on odd-numbered iterations of the outer loop (remember that the numbering starts from zero). The even numbered iterations save the stimulus to a variable ready to be used in the next trial.

---

<div class="post-metadata">

**Author:** ![unibrow](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@unibrow](https://discourse.psychopy.org/u/unibrow)\
**Post date:** [November 10, 2023, 5:35am UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/13 "2023-11-10T05:35:52Z")

</div>

Hi there, how does this code work for images? I’m a programming newbie and my input is a slider not a click answer like you have it. The respondents need to make a pairwise comparison between the two images.

In my case, I’ve already pre-combined the 6 images into 15 pairs so that they don’t appear twice (see Excel file below).  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/9/e9bd9696d3acca4ed80226484768b1b088fb0454.png)

What I’m currently stumped on, is how to randomize the relative position of the pairs (so that 001.jpg isn’t always on the left and 006.jpg isn’t always on the right, for example).

Is my strategy all wrong? Should I be importing each image in like OP did?

Thank you!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 10, 2023, 2:33pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/14 "2023-11-10T14:33:06Z")

</div>

Have a look at the suggestion I gave here: [Randomizing image location...kind of - #5 by wakecarter](https://discourse.psychopy.org/t/randomizing-image-location-kind-of/33237/5)

---

<div class="post-metadata">

**Author:** ![unibrow](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@unibrow](https://discourse.psychopy.org/u/unibrow)\
**Post date:** [November 10, 2023, 6:19pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/15 "2023-11-10T18:19:48Z")

</div>

Thank you! I got it to work, had to use locations[0] and locations[1] for position variables of the left and right image stimuli blocks. Cheers.

---

<div class="post-metadata">

**Author:** ![Herdias\_Hayyal\_Falah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/herdias_hayyal_falah/32/40910_2.png) [@Herdias\_Hayyal\_Falah](https://discourse.psychopy.org/u/Herdias_Hayyal_Falah)\
**Post date:** [February 19, 2024, 2:09pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/16 "2024-02-19T14:09:05Z")

</div>

Hello, i want to ask if i prefer to (a) what code should i write then? Thank you

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 19, 2024, 2:14pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/17 "2024-02-19T14:14:08Z")

</div>

Have a column for the target and another for the foil. Use locations[0] for the target location and locations[1] for the foil location. (where locations = [[-.3,0],[3,0]] and shuffled)

---

<div class="post-metadata">

**Author:** ![Herdias\_Hayyal\_Falah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/herdias_hayyal_falah/32/40910_2.png) [@Herdias\_Hayyal\_Falah](https://discourse.psychopy.org/u/Herdias_Hayyal_Falah)\
**Post date:** [February 19, 2024, 2:35pm UTC](https://discourse.psychopy.org/t/how-to-randomise-pairs-of-images/31966/18 "2024-02-19T14:35:30Z")

</div>

Sorry, i still not get it. Where should i write or make them. On excel or any else? I’am just first time doing psychopy, sorry…
