What do I get wrong with loopType: random?

URL of experiment:
https://run.pavlovia.org/fabianbross/randomization_question

Description of the problem:
I posted a similar question some time ago but did not get an answer and I’m starting to get crazy :sweat_smile: My issue is probably very simple to solve, but I’m not able to wrap my head around this :upside_down_face:

Very briefly: If I set the loopType to “sequential” everything runs as it should, but if I set the loopType to “random” my experiment is not balanced (see below) anymore :upside_down_face:

More detailed: The task is simple: Participants will be presented with text stimuli and corresponding questions. Participants should see twelve stimuli, six from one and six from another condition (this is what I meant by “balanced”). Stimuli should be chosen randomly from a list, i.e., an Excel sheet. In addition, they will see a bunch of fillers. I have an Excel file which looks like this (whether an x or y stimulus is chosen should be random):

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 added the following code to Begin Experiment (but note that I’m really bad at programming :sweat_smile: :rofl:):

//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"];
//shuffle the list so that whether conditionone or conditiontwo is shown is random.
util.shuffle(newlist);

//Then they will see sixteen fillers
fillerlist  =
["conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo",
"conditionone", "conditiontwo"];
//No need to shuffle the fillers

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

So now I have a list of all stimuli I want to present named “cuelist”. To Begin Routine I added a code which takes one element from this list:

//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
stims=window[stims];

If I run the experiment with loopType set to “sequential” everything works just fine, i.e., participants see six stimuli from the first and six stimuli from the second condition (and they are randomly chosen) :slightly_smiling_face: However, if I set the loopType to “random” this does not work anymore. Sometimes four stimuli from the first condition are shown and eight from the second or five from the first and seven from the second. Meaning: My balancing is gone.

Probably (and hopefully) I’m overlooking something really simple and stupid :sweat_smile: I would be really, really greateful if someone could point me into the right direction :slightly_smiling_face: :slightly_smiling_face: :slightly_smiling_face:

Hello,

why do you want to set the loop to random? You are already randomizing your stimuli by code. There is no need to set the loop type to random.

You have not yet randomized filler and stimuli. So instead to randomizing newlist, randomize cuelist.

Best wishes Jens

Thank you for your reply! :slightly_smiling_face: :slightly_smiling_face: The stimuli are randomized, that’s true! But now participants first see all the target stimuli and then all the fillers :sweat_smile: But I want that the target stimuli and the fillers are randomly mixed.

Hello fabs,

shuffle the combined stimulus-list cuelist. As far as I understood your code your are only shuffling the stimuli, then you combine the stimuli and the filler such that the stimuli are always presented first, followed by the filler. So, if you shuffle the combined list, you should be fine.

Best wishes Jens

But if I do that I won’t get six target stimuli from one and six from the other condition. At least not necessarily, right?

I have the feeling that I need to tell PsychoPy to first take out the stimuli from my Excel sheet and then randomize them. If I use “shuffle” or use the loopType “random” my list is shuffled and then PsychoPy looks at my Excel sheet and takes out the values.

Hello fabs,

sorry, I thought your code was doing that. I am not good at reading PsychoJS.

newlist contains six items of conditionone and six items of conditiontwo, filler contains 16 fillers, if you combine that in a new list, shuffle that, you are done, aren’t you.

Best wishes Jens

Hello fabs,

from your example above, you seem to have exactly 16 fillers and 12 experimental stimuli. In this case you simply have to set the loop-type to random. Could it be that you did not provided all information needed? So, do you have more than six stimuli from conditiona in your stimulus list? How does conditionsa and conditionone related to each other?

Best wishes Jens

I tried what happens if I shuffle newlist. But that doesn’t work :sweat_smile:

The problem is that the code creates a list of “conditionone” and “conditiontwo” which are simply the headers of the Excel sheet. PsychoPy only later looks into the Excel sheet and takes the values out of it one by one (so it doesn’t distinguish between stimuli and fillers). If I shuffle newlist there won’t be six items from conditionone and conditiontwo anymore. My approach might simply not be the right way of doing this :sweat_smile: :see_no_evil:

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

Hello fabs,

ok, you need to explain your intended experiment to me, like in a methods-material section.

How many stimuli do you have for each condition, how many stimuli does a participant see?

Best wishes Jens

Thank you that you try to wrap your head around this! :smiling_face_with_three_hearts:

Twelve pairs of sentences were created (so twenty-four in total). They are pairs: Half of them contain a rhyme, the other half does not. Participants will see six rhyming (“conditionone”) and six non-rhyming sentences (“conditiontwo”) which are randomly chosen from the twelve pairs. For each sentence participants see a control question which asks something about the sentence.

In addition, participants will see sixteen filler sentences (again, with corresponding questions).

I hope that helps :sweat_smile:

I’m trying to be more concrete. Here are some made-up stimuli:

conditionone conditiontwo question
cool and fool cool and lamb Did you see the word lamb?
hey and they hey and fish Did you see the word fish?
house and mouse house and dog Did you see the word mouse?
tea and sea tea and bag Did you see the word bag?
bag and drag bag and mouse Did you see the word mouse?
Conny and Ronny Conny and Mike Did you see the word Mike?
dog and fog dog and cat Did you see the word cat?
cat and rat cat and dog Did you see the word dog?
roll and goal roll and fog Did you see the word fog?
mice and nice mice and roll Did you see the word roll?
one and gone one and two Did you see the word two?
there and bear there and here Did you see the word here?
Filler sentence one Filler sentence one Did you see the word one?
Filler sentence two Filler sentence two Did you see the word two?
Filler sentence three Filler sentence three Did you see the word three?
Filler sentence four Filler sentence four Did you see the word four?
Filler sentence five Filler sentence five Did you see the word five?
Filler sentence six Filler sentence six Did you see the word six?
Filler sentence seven Filler sentence seven Did you see the word seven?
Filler sentence eight Filler sentence eight Did you see the word eight?
Filler sentence nine Filler sentence nine Did you see the word nine?
Filler sentence ten Filler sentence one Did you see the word ten?
Filler sentence eleven Filler sentence eleven Did you see the word elven?
Filler sentence twelve Filler sentence twelve Did you see the word twelve?
Filler sentence thirteen Filler sentence thirteen Did you see the word thirteen?
Filler sentence fourteen Filler sentence fourteen Did you see the word fourteen?
Filler sentence fifteen Filler sentence fifteen Did you see the word fifteen?
Filler sentence sixteen Filler sentence sixteen Did you see the word sixteen?

So far, my code generates a list of sixt times “conditionone” and six times “conditiontwo”. Then I randomize their order. I call this object “newlist”. These later correspond to the first twelve rows of my Excel sheet. Then it takes sixteen times “conditionone” and “conditiontwo” which are the sixteen remaining rows. I call this “fillerlist”. Then I combine these lists to “cuelist”. Let’s assume “cuelist” looks as follows:

[“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditionone”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”,
“conditiontwo”]

This would result in an experiment in which a participant will see six items of conditionone and six of conditiontwo and then the fillers.

If I shuffle “cuelist” there won’t be exactly six items from conditionone and six from conditiontwo anymore. The same happens if I set the loopType to random (which I did not expect). The reason is that I do not shuffle stimuli, but only pointers to whether PsychoPy should take an item from column “conditionone” or column “conditiontwo”.

Hello Fabs

I saw your post after I have finished this

Stimuli.xlsx (9.0 KB)
RanStim.psyexp (10.4 KB)

Best wishes Jens

Oh thank you so much!!! :star_struck: :star_struck: :star_struck:

Your experiment works fine on my computer. The online version didn’t work for some reason. But I slightly altered the code and now it works! The JavaScript code I now use (in a code component at Begin Experiment):

index1 = [0,1,2,3,4,5,6,7,8,9,10,11];
index2 = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
index3=[24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39];

util.shuffle(index1);
util.shuffle(index2);
index1 = index1.slice(0, 6);
index2 = index2.slice(0, 6);
index1.push(...index2);
index1.push(...index3);
util.shuffle(index1);

Thaaaank you! :dizzy:

Hello fabs,

I know. The code needs some manual PsychoPy to PsychoJs translation.

array.extend(array2)

does not auto-translate to

array.push(...array2)

First, get the experiment running offline, then manual adaptation for the offline version. I am glad that it worked as intended.

Bets wishes Jens

Hello Fabs,

I tried a different solution than the one above that fits more to your stimulus-file.

Fabs.psyexp (16.2 KB)
Fabs.xlsx (10.5 KB)

Best wishes Jens