Assigning participants randomly to a condition

@dvbridges That worked perfectly! Thank you so much!

I have just realized something important. The code that I wrote from your example had just condition A and B. However, I have 4 different conditions. Is the following correct code for 4 different conditions? I couldn’t be sure about JavaScript translation (about the brackets and numbers).

 *# Begin Experiment*
import random
condition = random.choice(('A','B','C','D'))
// Begin Experiment
function random_character() {
    var chars = "ABCD";
    return chars.substr((( Math.floor(Math.random() * 4), 3), 2), 1);
}

condition = random_character();  // the condition variable will be used in your loop, as with the Python code

@emregurbuz, try:

function random_character() {
    var chars = "ABCD";
    return chars.substr( Math.floor(Math.random() * 4), 1);
}

@dvbridges thank you so much for the help!

I am replying late because I couldn’t pilot the final version of the experiment online due to the Pilot token missing error. I will try to fix that problem and use the Javascript code that you provided!

All best,

Emre

Hi @dvbridges where should I put this code component? in a new routine at the beginning of the whole experiment? or in the first routine of my block?

You only want it to run once so it should be before your loop. It doesn’t need to be in a new routine if you already have one available (e.g. instructions).

I have 3 instructions screens (one after the other one) followed by one loop (three routines in this loop). The conditions file that I want to have pulled at random is in the loop. All instructions would be the same for participants.

Should I then put it inside the first routine inside my loop? or in the last set of instructions?

Since it’s in a Begin Experiment tab of a code component it shouldn’t matter, but I quite often put everything like this into the very first routine so it’s easy to find.

1 Like

Is there a possibility to randomize people to a condition based on their response on a slider or something else? so based on a response, that they gave before the specific instructions for each condition?

Many thanks for your solution! The code works well for my study as well. But I have a further issue which I guess a lot of people might also have: how to ensure that an equal number of participants are randomly assigned to each of the two versions? Is this code alone able to do that?

To my knowledge there isn’t a way to make sure # ptps is equal across conditions when using code to randomly select a condition.

One option that I’ve yet to try, but others seem to have had success, is using @wakecarter’s tool here to assign consecutive ptp ID #s, and then within the url that you send to pavlovia, use “%x” where x is the number of conditions. I believe it works by taking the number of the current ptp, e.g. “11”, and dividing by the # of conditions and returning the remainder as the condition assignment, so that if you had 3 conditions, ptp ID #11 would be assigned to cond 2.

You could also just use random as you’re currently doing and tally on your end, but of course a bit more burdensome.

1 Like

Thank you very much for the suggestion! I’ll try the code and see.

@dvbridges I have a question. I have 4 conditions: ABCD. I want the participants assigned randomly to one of the four conditions. According to conditions, most of the parts of the experiment will be changed such as instructions, emotional text, or some rating sentences.

I used this code and I create 4 different condition files: OrderA.xlsx, OrderB.xlsx, OrderC.xlsx, or OrderD.xlsx. I indicated the part of the changed with condition in both condition file (OrderA.xlsx etc.) and and routine (like this $instructions).

Actually, the code perfectly worked first. Then, PsychoPy has begun showing both one experimental condition (for example OrderA) and None (twice or three times). I haven’t any “None” condition, but it shows an empty condition/screen with constant properties in the experiment for twice or three times.

I am not sure where I have a mistake.
Thanks in advance.

Windows 10
PsychoPy 2022.1.3
Online Experiment