Assigning participants randomly to a condition

Windows 7
PsychoPy 3.0.0
Standard Standalone:

I want to assign participants to one of two possible conditions randomly. So far I am able to assign participants to a condition manually by using ‘group’ in the info dialog. That is, the conditions for each trial are defined by: $expInfo['group']+".xlsx", which works fine. However, it requires me to manually define the group for each participant. I want to run the experiment online, and therefore have participants be automatically, and randomly (not fullRandom) assigned to one of the groups.

I also tried using an outer loop for the entire experiment, which worked for randomizing ‘A’ and ‘B’, but forced it to run both ‘A’ and ‘B’. It seems it might be possible to short-circuit a loop, but it feels like there should be an easier solution to this problem.

There are two versions of my experiment that use the exact same trials, but different stimuli. I want to assign participants randomly to each version online.

I tried

  • Using the Info Dialog to determine group - but this had to be done manually
  • Using a block loop for the entire experiment - but this forced participants to run both versions

I hope this is sufficiently clear. Please let me know if any additional info is necessary. I plan on using Pavlovia to run the experiment in the end, but I have not tried it yet.

Thanks in advance.


@Nessimon, in a code component you could add the following:

# Begin Experiment
import random
condition = random.choice(('A','B'))

Then, your filename in your loop would be called $'Order' + condition + '.xlsx', which would give OrderA.xlsx, or OrderB.xlsx. This will work for Python, but for running the study online you will need JavaScript code - remember to switch the code component to Code Type “Both” to see both JS and Python code panels. Here is some JavaScript…

// Begin Experiment
function random_character() {
    var chars = "AB";
    return chars.substr( Math.floor(Math.random() * 2), 1);

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

This works perfectly. Thank you very much.

Thanks for that comment. I am trying to use this solution, however I have troubles indicating several xlsx conditions files in the loop. Could you please advise what am I doing wrong?
In the “Conditions” section in my loop I write $“Block” + condition + “.xlsx” (I have two conditions files: BlockA.xlsx and BlockB.xlsx). But Builder is unable to find my conditions files.

What is the path to your conditions files?

They are in the same folder as my experiment

Maybe someone can recommend a demo where participants are randomly assigned to conditions?

Could you provide the full content of the error message?

Thanks Michael, it looks like that


Have you actually tried running the experiment?

At the design stage, that warning itself is nothing to worry about, because the condition variable doesn’t exist until after the experiment starts. So it is natural at that stage that Builder doesn’t yet know where the file will be.

NB This particular text is not an error, just a warning. An error is something that stops the experiment from running.

1 Like

Thanks Michael! Unfortunately the Ok button is unable (as you can see from my screen), so I am just not able to close the loop because of it and run the experiment…

I tried several times and eventually ok button was enabled and it worked for some reason!
Thanks a lot!

@dvbridges thank you for your suggestion! I had really similar question to @Nessimon and your suggestion worked perfectly for me on my laptop! However, I couldn’t run the experiment on Pavlovia. I may be doing something wrong about Javascript.

Windows 10

I have 4 different condition file and all of them have different response keys combination, therefore, I have 4 different instructions. Since I will do the experiment on Pavlovia participants need to be assigned to one of the four conditions randomly.

The following is the flow of my experiment;

After participants are welcomed I want to present them one of the 4 different instructions. To do this I added the code you provided as following to the instruction routine and then added image component (named instructionss).

Image name is $'Order' + condition + '.png' and set to set every repeat.
Loop name is $'Order' + condition + '.xlsx'

Although my Exp. works perfectly on my desktop I couldn’t manage to run it online. I have following error (I interpreted as I had a problem with my Javascript) However, I couldn’t solve my problem.


Is it possible to add a code component out of flow just like I did?

Also I have a feedback component. Is it possible that codes overlap? Following is my feedback code:

Thanks in advance!

@emregurbuz, the error suggests your code has not compiled correctly. You should have had an error message output into the Runner window when you tried to sync your experiment, that will point to where the error has occured.

@dvbridges thank you for the super fast reply!

Following is the error message output I received when I tried to sync.


I guess the problem is with the feedback code. Following is the HTML file that I opened on github:

Also the first lines;


Many thanks,

@emregurbuz, its probably because you need to be in a loop to use continue - it moves on to the next loop iteration. In this specific code, you do not need the else continue statement.

I want to give feedback after each trial to participants by showing a red X if they make a mistake. Since I do have my feedback code component in the loop, I thought I had to use “continue” (or equivalent in JS as well). How can I do this in Javascript?

Please excuse me if I am asking elementary questions, I am a newbie in coding.

I see, you could try something like this, putting the code in the relevant tabs:

// Begin Experiment
red = new util.Color("red");  // create new colors
green = new util.Color("green");

// Begin Routine
if (resp.corr === 0) {
  msg = 'X';
  text_5.color = red;
} else {
  msg = 'Correct';
  text_5.color = green;

Ive used text_5 as the text component, since in your example it looks like that is the feedback component.

@dvbridges yes I see! So the problem is that I cannot use continue in JS as I use in Python!

Thank you very much!

I am curious about one last thing. Is it possible to give feedback only on erroneous trials (i.e., “mistake” when person makes an error and “No response” when one does not give any response" but not providing any feedback (i.e., correct), and instead simply continue to routine?

Sure, try the following

if (resp.corr === 0) {
  msg = 'X';
  text_5.color = red;
} else {
  msg = '';