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 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
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.
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.