Random assignment of participants to groups

Hey all!

I am trying to set up a PsychoPy experiment, first time all by myself.
Unfortunately, I am a total beginner when it comes to Python and programming.
Good thing is, I already have a working experiment template from a previous pilot study which I now need to change to my needs.

Major thing which needs to be included is: Participants should randomly be assigned to one of four different groups. The experiment consists of two phases: training phase and generalisation phase. Thus, when a participant was assigned to group 1 in the training phase, they have to be assigned to the same group in the generalisation phase.

I’ve created the files I need (excel & folder with pictures) for each group and each phase: so I have for example the files for TrainingG1 (group 1) containing the TrainingG1 excel file and the TrainingG1 zip-folder with the PNG files. Same goes for TrainingG2-4 and GeneralisationG1-4.

I know how to set up the experiment with Training & Generalisation phase for one group, but how can I add a “junction” so to say where the participants are randomly assigned one way (either group1, group2, group3, group4)?

I would really appreciate your help!

Hello Lucia

if int(expInfo['participant']) %4 == 0:
    stimfile = "stimfile4.xlsx"
elif int(expInfo['participant']) %3 == 0:
        stimfile = "stimfile3.xlsx"
elif int(expInfo['participant']) %2 == 0:
        stimfile = "stimfile2.xlsx"
        stimfile = "stimfile1.xlsx"

Put this code at a “Begin Experiment”-tab. It loads four differtent stimulus-files depending on the participant number.

Best Jens

Hey Jens and LuciaIglhaut,
two things:
Do you know of a way to group participants with non-numeric IDs? I am using Prolific (for the first time) and I believe they don’t use a simple incrementing number as ID.

and for Lucial:
once you used the Code of Jens, you need to use the variable (in this case stimfile) in the conditionsfile of a loop. like this: $stimfile
it is necissary, that all of the variables in the .xlsx are called the same and is kind of prone to errors.


Hello Becker,

sorry, I have not used Prolific, but you find suggestion on how to use Prolifc and Pavlovia in the forum Search results for 'Prolific' - PsychoPy

Best Jens

1 Like

Hi @JensBoelte

I’m afraid that this code is incorrect. For four groups all of the statements should have %4 but then check for values 0, 1, 2 and 3.

You can use my VESPR study portal to assign participants to groups or assign numeric participant numbers.


Hi @wakecarter

do you mind telling me what is wrong with this code? I checked it and it loads the simulus-files accordingly.

Best wishes Jens

Hi Jens,

In words, you’ve written: if divisible by 1 else if divisible by 3 else if divisible by 2 else.

Here are how the first 20 participants will be divided:

  1. 1; 5; 7; 11; 13; 17; 19 = 7 participants
  2. 2; 10; 14 = 3 participants
  3. 3; 6; 9; 15; 18 = 5 participants
  4. 4; 8; 12; 16; 20 = 5 participants

Please correct me if you think I’ve got that wrong. Participant 0 (if you have one) would be allocated to group 4.

My method is to divide by 4 and use the remainder which will equally be 0, 1, 2 or 3.

1 Like

Hello @wakecarter

that is correct. My code does not result in an even distribution of participants to all groups. I knew that. Your code results in an even distribution of participants to groups which is probably advantageous in most cases.

Thanks for highlighting this.

Best wishes Jens

1 Like