Simultaneously randomize two columns in conditions file

What are you trying to achieve?:

I’m trying to randomize two columns in my conditions file, one sound and one image, so that they do not always occur in the same pairs.

E.g. sound 1, 2, 3, 4 and image a, b, c, d not always occurring in the combinations 1-a, 2-b, 3-c, 4-d.

What did you try to make it work?:

I know this is a issue that has been brought up multiple times in the forum.

I have tried the 2012 solution offered here with the following code:
https://groups.google.com/g/psychopy-users/c/WljgXeoy6Ik

import random
import csv

first=[1,2,3,4]
second=[“A”,“B”,“C”,“D”]

firstRand=random.sample(first,len(first))
secondRand=random.sample(second,len(second))

with open(‘order_file.csv’,‘wb’) as w:
write=csv.writer(w)
writer.writerow([‘first’,‘second’])
for i in range(len(firstRand)):
writer.writerow([firstRand[i],secondRand[i]])

What specifically went wrong when you tried that?:
Not only does this solution not work for me (NameError: name ‘writer’ is not defined) but it’s also unclear to me how it is supposed to work.

I’m not using a csv file but an excel file as my conditions. Am I supposed to enter the names of my files in the first= and second= lists? I’m using over a 100 files so I’d like to avoid this.

I’m hoping there is a more recent solution to this problem, as I know it is a common one.

Why do you have 100 spreadsheets?

Sorry, I was referring to over 100 sound files and images. Ideally, I’d use only one excel file.

I’ve just written a demo called Independent Randomisation that should help.

Thanks very much for creating this demo!! I’m sure it will help many other users too.

I’ve got a question and am receiving an error message when trying it out.

First, my error:

listA.append(str(int(grid)))
ValueError: invalid literal for int() with base 10: ‘myimage.png’

I suppose I’m receiving this error because the names of png files that the code is reading in are not simply integers. Is there a way to solve this without changing all of my file names?

Now the question:

Just to be sure, I don’t need the code in the ‘trial’ routine, do I? That is simply to rate the answers as correct or incorrect and give feedback at the end, right?

Thanks again for your help.

EDIT:

I changed the code to

listA.append(str(str(grid)))

and now I can run the experiment without an error message. I’ll leave the error message in case it helps anyone reading this in the future.

Sorry for the confusion. My code was coping with translating numbers to strings without ending .0. You just need listA.append(grid)

In the trial routine you do need thisA=listA.pop() and thisExp.addData(‘thisA’,thisA) to select and record the value being used.

So, it is kind of working for me - the sound and image do not appear in the same combinations anymore. :partying_face:

However, the images (grid - what I put in the place of your colA) always occurs in the same order.
I suppose this is simply how your code works (while the one column is randomized, the other is not thus resulting in random pairs)?

While this is better than before, it is not ideal for the purposes of my experiment. Is there anyway to randomize colA as well, but separately from colB?

Check that both of your loops are set to random. Mine are.

Yes, all of my loops are set to random.

I also tried using your experiment and simply loading in my conditions file and changing colA to grid, etc. and have the same result.

Sorry - false alarm!

For whatever reason, it’s working now.

Thank you very much!