Javascript code for random selection of condition

My study assigns participants to one of two conditions (could become one of four, so I don’t really want a coin-flip routine, despite my variable name).

I have working Python code, this is no problem:

import random
coinflip = random.randint(0,1)
if coinflip == 0:
    assignedGroup = "setABlocks.csv"
else:
    assignedGroup = "setBBlocks.csv"
    

But since I want to run it in Pavlovia, I need the Javascript version too, but the things I have tried (from searching the web) are not working at all. Here is what I have:

import java.util.Random;

Random random = new Random();
int output = random.next(100);

if(output > 0 && output < 50) {
    assignedGroup = "setABlocks.csv";
}
else if(output >= 50 && output < 100) {
    assignedGroup = "setBBlocks.csv";
}

Is there someone here who can help, please? I can’t Java, and Pavlovia gives no hints as to where I am going wrong, it just gets stuck on initializing the experiment, which I understand is from having a JS issue.

Hi Pauls,

Try this:

coin_flip = Math.random();

// pick one condition randomly
if (coin_flip > .5) {
    assignedGroup = "setABlocks.csv";
} else {
    assignedGroup = "setBBlocks.csv";
};

I think that should work.

1 Like

Thank you so much :smile: - the solution was simpler than I thought.

1 Like

Hi @Pauls12,

I want to do the same, where in the code component do you write this code?

and what things would I have to change if I want to do it with 8 lists?

Thanks so much!

Hi Laura,
There is an option in the code component to have “Code Type” as Py, Js or Both. Select “Both” and you will have side-by-side windows, one for the Python code and one for the Javascript code.

To do this with 8 lists, you would have one ‘if’ statement, 6 ‘else if’ statements, and 1 ‘else’ statement to assign the intervals. To build from the examples where I was assigning to csv file names:

die_toss8 = Math.random();

// pick one condition randomly
if (die_toss8 < .125) {
   assignedGroup = "set1.csv";
} 
else if (die_toss8 >= .125) && (die_toss8 < .25) {
    assignedGroup = "set2.csv";
} 
else if (die_toss8 >= .25) && (die_toss8 < .375) {
    assignedGroup = "set3.csv";
} 
else if (die_toss8 >= .375) && (die_toss8 < .5) {
    assignedGroup = "set4.csv";
// et cetera until the last one
} 
else {
    assignedGroup = "set8.csv";
};

Definitely double check my code & numbers, or maybe someone else will correct me but I hope this helps!
Cheers,
Paul

1 Like

Thank you @Pauls12 I will definitely try this out.

You don’t need to do something as complicated as this for selecting a random condition or a random number from 1 to 8.

You can either start with a list of conditions

conditions = [‘set1.csv’,‘set2.csv’,‘set3.csv’,‘set4.csv’,‘set5.csv’,‘set6.csv’]

and then
shuffle(conditions) and use conditions[0] or set assignedGRoup=conditions[0]

or you can use randint(1,8) and assignGroups as above.

In order to use shuffle and randint online, you need a code_JS component as per my crib sheet. https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit?usp=sharing

Best wishes,

Wakefield

3 Likes

Nice, thanks!

Hi @wakecarter ,

I have 1 trial list containing 48 different images. I want to download this resource at the start of the experiment, randomly shuffle the images, select the first 30 images, and save it as a new trial list to be used in the task. Is that possible to implement? I looked at your crib sheet but I am not sure where something like this is located.

Thanks.

Why do you want to save it as a separate trial list? Is that because you are going to use 30 out of 48 several times. If you are only going to use 30 out of 48 once you could just end the loop after 30 iterations.

If you really need to do what you’re asking then you can load the images into an array in one loop and then have a separate loop in code to append 30 items from it to a second list.

In one case, there is over 250 images to select from so I am dynamically load the images instead of downloading all of them at the start. So, ideally, it would read the trial list, shuffle, select first 30, save new trial list with the 30, and dynamically download the 30 images.

Here’s a example of some JS code doing something similar.

shuffle(naturalSpeech);
shuffle(repeatedSpeech);

wordsLeft = naturalSpeech.length;


psychoJS.downloadResources([
{ name: (naturalSpeech[wordsLeft-1]), path: ("../CS/" + naturalSpeech[wordsLeft-1]) },
{ name: (naturalSpeech[wordsLeft-2]), path: ("../CS/" + naturalSpeech[wordsLeft-2]) },
{ name: (naturalSpeech[wordsLeft-3]), path: ("../CS/" + naturalSpeech[wordsLeft-3]) },
{ name: (repeatedSpeech[wordsLeft-1]), path: ("../SS/" + repeatedSpeech[wordsLeft-1]) },
{ name: (repeatedSpeech[wordsLeft-2]), path: ("../SS/" + repeatedSpeech[wordsLeft-2]) },
{ name: (repeatedSpeech[wordsLeft-3]), path: ("../SS/" + repeatedSpeech[wordsLeft-3]) }
]);


It should be possible to set up a loop to create a list of dictionaries and then download the list. Don’t try to issue the download command 30 times.