| Reference | Downloads | Github

Variable randomisation


I’m wondering if there is a way to randomise participants based on information they provide earlier on PsychoPy. For example, I’m looking to randomise based on sex, ideally to have equal numbers of males/females in each condition. So, participants may enter ‘male’ using text entry code earlier. Is it possible to do this? The reason why is so that the study is double-blind.


Anything is possible but sometimes doing this sort of thing in code can end up being a bit inflexible, but as you say, can help achieve double-blinding so can be an avenue worth pursuing.

You would need to provide more details of your design (e.g. number of subjects, and what you mean by “each condition”). And does the experimenter not know the gender of the participant? i.e. is it vital for the participant to enter this information, or can the experimenter do it?

Hi Micheal,

It is likely that the experimenter will know the gender of participant, but ideally the participant would enter this themselves on PsychoPy. Ideally PsychoPy could randomise a given participant, based on what they enter for gender, to one of four conditions. E.g. If there are 3 females and 1 male in one condition, and 1 female and 3 males in another condition, PsychoPy would place a female participant in to the latter, so as to equal the conditions out. Could PsychoPy perhaps read back the information a participant inputs (gender) in the ‘pop up window’ when you press run to decide which condition? Perhaps with logical operators in the code?

We hope to recruit around 88 participants. The four conditions are all exactly the same in terms of the flow, apart for minor alterations/additions to particular routines. Therefore, for the routines in which the conditions differ, they will likely be attached using an Excel file on a random loop.

I think what you try to achieve is possible, but I’m not sure if you can do it with the builder alone.

You can add fields to the expInfo dialog when you click on Experiment Settings in the builder. If you add a field gender with the default values ['male', 'female'] and compile the script, you will get a line similar to this

expInfo = {'participant': '', 'session': '001', 'gender': "['male', 'female']"}

in the coder view. If you change this to

expInfo = {'participant': '', 'session': '001', 'gender': ['male', 'female']}

(so without the quotes around the square brackets), you will be able to choose the gender on the experiment startup.

@Michael do you know if/how this can be achieved in builder alone?

In your TextStim Text box you can put some logic to use that variable. Let’s say you have a conditions file my_conditions.xlsx that looks like this

male female
condition1_male condition1_female
condition2_male condition2_female
condition3_male condition3_female

in your TextStim Text box you could write $my_conditions[expInfo['gender']] to choose a column dependent on the gender variable.

Sorry if this is confusing or not what you needed.

1 Like