# Randomizing each column separately

Edit: I’ve updated the code to a version that works offline. My question is now how to adapt this to Pavlovia.

I’m very new to PsychoPy!

Very simply, I have an experiment that shows a prompt, and two response options. I would like the pairing between prompts and response options to be randomized for every participant.

I’ve set up a condition file with three columns: Prompt, Option1, Option2

I have three text objects on each trial, one for each of these.

What I’m unsure about is randomizing the pairing of jobs and candidate names. And the added difficulty is that the two names should stay a pair. For example, if Name1 = Bill, and Name2 = Fred, even if that pairing goes with a different job for each participant, I would always like Bill and Fred to go together.

I had been following the approach from this video, which resulted in the code below. However, I’ve read that this approach can’t be implemented online.

I’d love any help you could offer to help this work on Pavlovia!

Begin Experiment:

``````import random, xlrd

#randomize seed
random.seed()

in_file = 'trials.xlsx'

num_items = 4

#counters to hold the next stimulus reference
cur_job = 0
cur_name1 = 0
cur_name2 = 0

``````

Begin Routine:

``````
#open excel file
inbook = xlrd.open_workbook(in_file)
insheet = inbook.sheet_by_index(0)

#arrays to hold our stimuli
job_stim = []
name1_stim = []
name2_stim = []

#loop through the rows
for rowx in range(1, num_items+1):

row = insheet.row_values(rowx)

#save the different values in that row you just loaded in
job_stim.append(row[0])
name1_stim.append(row[1])
name2_stim.append(row[2])

#shuffle the arrays
random.shuffle(job_stim)
## To Shuffle two List at once with the same order
mapIndexPosition = list(zip(name1_stim, name2_stim))
random.shuffle(mapIndexPosition)
## make lists separate again
name1_stim, name2_stim = zip(*mapIndexPosition)

``````

End Routine:

``````cur_job = cur_job + 1
cur_name1 = cur_name1 + 1
cur_name2 = cur_name2 + 1``````

I’ve a look at my independent randomisation demo on the online demos thread.

Thank you! I’m looking at it, but to be honest this is all a bit impenetrable to me. I’ve only been using the Builder window so far, and only got the above code from a walkthrough. Is there any chance you could point me to an article or video that walks a person through this kind of randomization?

Are you looking at it in Builder?

Oh! I actually didn’t realize that was a possibility, it is much easier now.

Am I right that it works like this (ignoring the side switching and correct answer recording for now):

• loadA loops through the list of numbers three times, in a random order, creating an array of the numbers 0-9, each three times, in a random order
• then on the trial, the letter is randomized from the excel file as normal, and the number is the “latest” (? I’m a bit confused on this point, how it gets a certain number, but that’s okay) number from the array

I think I understand that and can modify it for my experiment, thank you!

``````NameError: name 'psychoJS' is not defined
``````

because it seems to just be when I run it locally, and not on Pavlovia.

Edit2: I have an issue of the first trial always saying “Hello World” instead of presenting one of my stimuli? And when I look at the data, it says that it DID present a stimulus on the first trial.

Correct.

Your PsychoJS error is probably because you’ve put Python code in code_JS when it should only have JavaScript code.

Hello World can occur if your code component is below your text component. In general code components should be at the top of the routine.

Hello World can occur if your code component is below your text component. In general code components should be at the top of the routine.

Fantastic, that solved the problem. Thank you!