const ListMale = [1,2,3,4,5,6,7,8,9,10];
const ListFemale = [11,12,13,14,15,16,17,18,19,20]
const run1List = [];
const run2List = [];
//repeat for all 4 lists
//generate a random number to decide the ratio - 2m:3f or 3m:2f
var ratio = Math.floor(Math.random()*2)
if (ratio == 0) {
//List 1
for (var j=0; i<=1;i++) { //2m
for(var i=ListMale.length-1;i>=0;i--){
var temp = ListMale.splice(Math.floor(Math.random()*ListMale.length), 1);
run1List.push(temp[0]);
}
}
for (var j=0; i<=2;i++) { //3f
for(var i=ListFemale.length-1;i>=0;i--){
var temp = ListFemale.splice(Math.floor(Math.random()*ListFemale.length), 1);
run1List.push(temp[0]);
}
}
}
//you need to do the same for list 2,3, and 4 - each list is defining images to be shown in each run
else { //3m:2f ratio
//List 1
for (var j=0; i<=2;i++) { //3m
for(var i=ListMale.length-1;i>=0;i--){
var temp = ListMale.splice(Math.floor(Math.random()*ListMale.length), 1);
run1List.push(temp[0]);
}
}
for (var j=0; i<=1;i++) { //2f
for(var i=ListFemale.length-1;i>=0;i--){
var temp = ListFemale.splice(Math.floor(Math.random()*ListFemale.length), 1);
run1List.push(temp[0]);
}
}
}
What this does it gives you 4 lists, each containing the numbers of babies to be displayed in a run. It also gives you two arbitrary selected orders of m:f ratios (you can select whatever ratio you wish for each list in each of the two randomly selected scenarios). If you make sure that your name conventions follow a principle (e.g. baby1_1, baby1_2, baby1_3 where “_1” is always a sad baby, “_2” is always a neutral baby, and “_3” is always a happy baby) it will be no issue to assign the proper images. Or, you can also have string elements in the male and female lists and make them actual names of the source images.
This approach makes sure to never repeat the same baby as once it’s been selected, it removes it from the list of babies for that participant. If you needed, you could reshuffle elements of each list (run1List etc.) for further randomization within the lists as this would randomize the order of different babies to be displayed. You could do that by using Fisher Yates Shuffle (there’s other ways too):
function fisherYatesShuffle(arr) {
for (let m = arr.length - 1; m > 0; m--) {
const n = Math.floor(Math.random() * (m + 1));
[arr[m], arr[n]] = [arr[n], arr[m]];
}
return arr;
}
run1List = fisherYatesShuffle(run1List);