| Reference | Downloads | Github

Use numpy to flip a coin / breach a trial in online experiment

URL of experiment:

Description of the problem: Hello there, I have a rather specific problem with an experiment that I would like to run online. In my experiment, I first show a stimulus, then either a mask or nothing and finally a target in which participants are required to give an answer. I am trying to implement the either-or part, that is: either play a movie file of 2 seconds or show a text-component without any text in it for 2 seconds. On my computer, I found two ways to do this, but neither works online.

First one involves the coder where I flip a coin and set the status of the two components accordingly (for example: if coin == 0: movie.status = FINISHED, nothing.status = NOT_STARTED). Here, both the movie and the text component are part of the same routine. I cannot do that in the builder because the status of the components is set after the code from a code-component is inserted, thereby overwriting my changes.

The second solution works in the Builder. The movie and the text component are part of two different routines. Around each of these routines runs a dummy loop. Before the loops start, I flip a coin. According to this coin flip, the nReps of the DummyLoops are set to either 0 or 1 (following this tutorial ).

As mentioned above, both methods work locally (with the first one providing a nicer output-file), but neither works online. As far as I read, it is impossible to have coder-experiments online. As for the second, Builder-centered option: The browser is initialising the experiment forever.

So, here is my question: How could I have the running of a component or routine within a loop in an online experiment depend on a random variable that is set on every trial?
Any help is appreciated!

I would define an array called coin=[0,1]

Then shuffle(coin) each loop and have the nReps for your dummy loop being $coin[0]

Have a look at to see how to define a shuffle function.

Best wishes,


Hey wakecarter,

thank you for the suggestion, it was just what I needed! I tried it and it works fine on my Computer, and it also works online but with a strange new behaviour: the stimulus in the very first trial is not shown. On the following trials, one stimulus flashes up very briefly and is immediately replaced with another one. This only happens online though. Do you happen to have a suggestion for this or should I rather open a new question? Thank you again!

I assume that you already have the code component at the top of the routine. I sometimes find it’s a good idea to put the prep code in a separate routine so that everything is in place by the time the trial routine starts.

OK, I’ll try. Thank you for your help!

BTW, I’ve just written a self reference matching experiment (first draft) which has a pretrial and a preblock routine. It’s set to public