Randomization problem with JavaScript: Equal amount of stimuli from two conditions

Description of the problem:

I have an experiment with two conditions, fillers, and one question. I have an excel sheet which looks like this:

conditiona conditionb question
x1 y1 question1
x2 y2 question2
x3 y3 question3
x4 y4 question4
x5 y5 question5
x6 y6 question6
x7 y7 question7
x8 y8 question8
x9 y9 question9
x10 y10 question10
x11 y11 question11
x12 y12 question12
filler filler question13
filler filler question14
filler filler question15
filler filler question16
filler filler question17
filler filler question18
filler filler question19
filler filler question20
filler filler question21
filler filler question22
filler filler question23
filler filler question24
filler filler question25
filler filler question26
filler filler question27
filler filler question28

I want participants to be randomly presented with either a stimulus of “conditionone” or “conditiontwo”. This is easy. But I want that they see exactly six (text) stimuli from “conditionone” and six from “conditiontwo” (and then the corresponding question) randomly interspersed with fillers.

I created the following JavaScript code which I added into a code component (“Begin experiment”):

//Participants need to see six stimuli from condition one and six from condition two
newlist =
["conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo"];

//Then they will see sixteen fillers
fillerlist  =
["conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo"];

//Put the list of stimuli and the filler list together
cuelist = (newlist + "," + fillerlist);
//In the right format:
cuelist = cuelist.split(",").map(String);

To “Begin routine”, I added:

//Take the first element of cuelist:
stims = cuelist.shift();
//Write this to the data file:
psychoJS.experiment.addData("stim", stims);
//Evaluate the string as a variable

Then I added a loop with my excel sheet as an input and set the loop type to random. I added two text components, one taking $question and one taking $stims as an input (both: set every repeat).

The problem is: I do not get six stimuli from condition one and six from condition two, but sometimes like five from condition one and seven from condition two and stuff like that. I would really appreciate input! :sweat_smile: :slightly_smiling_face:

I have a suggestion: Implement this randomness through lists, for example:
**newlist,fillerlist,**The number of items selected from these two lists is not the same, so you can define the list:
count1 = 0
count2 = 0
List1 = [0,1,2,3,4]
List2 = [0,1,2,3,4,5]
And then extract the elements:
count1 += 1
count2 += 1
Then set a different number of cycles

Dear wen,
Thank you for your reply! Unfortunately, I’m not good at programming :sweat_smile: What exactly do List1 = [0,1,2,3,4] and List2 = [0,1,2,3,4,5] do? Where should I put this? And with cycles you mean the number of nreps in PsychoPy? You see that I do not really understand much :sweat_smile: :sweat_smile:

Now I realized the following: If I set the loopType to sequential everything works as expected. That is: Six stimuli from condition one are presented and six from condition two. But of course everything is presented sequentially :rofl: I thus guess that I somehow misunderstood what random does. There must be a simple solution I’m not seeing :sweat_smile: