| Reference | Downloads | Github

How to control next trial in loop?

Hello, I have this Excel file. As you can see it has a field called stimType which has 0 or 1 value. My loop is random. It works but I want to change something, I don’t want two stimType = 1 after each one so if stimType = 1 the next trial should be stimType = 0. How can I do that ? :blush:

RedPriority.xlsx (8.9 KB)

So effectively you want this variable to alternate from 0 to 1, which means it can’t be controlled within your conditions file, as that is being presented in random order. So you could use code instead to generate a value on each trial. For example, set stimType to be 0 on even-numbered trials and 1 on odd-numbered trials. To do this, insert a Code Component from the “custom” component panel. In the “Begin routine” tab, insert something like this:

# set stimType to alternate from 0 to 1
# on even- and odd-numbered trials:
stimType = your_loop_name.thisN % 2

# save the value in the data file:
thisExp.addData('stimType', stimType)

The % or modulo operator returns the remainder of dividing the current trial number by 2, which will be 0 for even-numbered trials (including the first one, which is number 0), and 1 for odd numbered trials.

You can then refer to this variable elsewhere, but arrange this code component to be above any other components which refer to it, so that they get access to the value which has been calculated for the current trial.

1 Like

@Michael Thanks Michael, but there is a problem, only if stimType == 1 next one should be stimType == 0, but if stimType == 0 appeared, the next one can be 1 or 0 again.

That could be difficult to achieve deterministically with balanced numbers of 0s and 1s, with any reasonable length sequence. You might need to use some sort of brute force algorithm that would evaluate candidate sequences until you find one that fits your constraints.

Or is it OK for these numbers to be unbalanced?

1 Like

Sorry for my delay dear @Michael, unbalanced is okay and there is no problem. I just don’t want two stimType == 1 continues and everything else is okay. Thanks for replies. :heart_eyes: