Randomize duration of a Routine in Builder

Hi!

Participants are presented with pictures and then fixation cross. I need to set duration for the fixation cross from 3 to 3.5 seconds. It should be random each time. I am a complete newby in Python and already tried several codes proposed on the forum. Unfortunatelly, they do not work for me.

Could someone suggest a solution?

Hello,

you need to insert a code-component preceding the text-component that you use to set the fixation cross. In the code-component initialise a variable in the Begin Experiment tab for the fixation-duration

fixDur = 0

In the Begin Routine tab enter the following code

fixDur = 3 + (3.5 - 3) * random()

You might want to add the following line in the End Routine tab.

thisExp.addData("FixDur", fixDur)

This adds a column containing fixDur to your result file. In the fixation text-component set the property duration to $fixDur.

Notice this solutions gives rather fine-grained fixation durations. The function random generates a random number between 0 and 1. And we simply setting a lower and upper boundary with min+(max-min).

You can test this in Excel. the function to generate random numbers in Excel is called rand().

Best wishes Jens

1 Like

Thank you for the answer!

I get this error, when I try:

if tThisFlipGlobal > text.tStartRefresh + asarray($fixDur)-frameTolerance:
^ (arrow on the dollar sign)
SyntaxError: invalid syntax

Experiment ended.

The dollar sign should not appear in code components

It is only in the text component settings. I also tried ‘duration (frames)’, ‘frame N’ and ‘condition’.

Hello,

there is no need to use “set every repeat” if you don’t change the stimulus. It can be constant.

Which psychopy-version are you using? I’m using 2020.2.10

Best wishes Jens

No, a more recent one: PsychoPy 2021.1.2

Hello,

what happens when you set your PsychoPy to use an older version (edit experiment settings → use PsychoPy version)?

Best wishes Jens

Error:

if bool(asarray($fixDur)):
                ^ (arrow on dollar sign)

SyntaxError: invalid syntax

Experiment ended.

Hello,

does this RandFix.psyexp (13.5 KB) run?

Best wishes Jens

Yes, it looks like this:

And gradually counts down/

Hello,

looks good. It is not a count-down. :wink: The number in middle stays on screen for a period lasting between 3 - 3.5 sec. How “large” is your experiment? If it is not too large, you could use this example to build your experiment.

Best wishes Jens

it’s from 250 to 500 stimuli. is it too big to use what you’ve sent me?

@JensBoelte means in terms of numbers of routines.

500 stimuli could still just be one routine with a couple of components in one loop.

Thank you. When I copy-paste your code in my experiment, weirdly it refuses to work. Again because of $ dollar sign in the duration line. I guess the only chance is to rebuild my experiment in your file. Sounds like nonsense, but we have what we have :frowning:

Have you tried without the dollar sign?

oh my! it finally works. thank you both @JensBoelte and @wakecarter very much :clap:

When I follow you steps, it can visualize the stimulus, but once the time is over, the whole experiment stops and does not go to the next step.
I have the following error:

Any ideas?

Thank you very much :slight_smile:

Do you have movie.stop() or similar in a code component? Can that code be reached before the movie is first started?

Hi @wakecarter

Thanks for your reply. I don’t have a movie.stop() or similar in my code component.
In the ‘Begin Routine’ I have:

sstim_dur =[2,3,1.5]#[20,24,28,32,36,40,44,48,52,56,60]#random()*(3.5-3)+3
shuffle(sstim_dur)
stim_dur = sstim_dur.pop()

and in the ‘End Routine’ I have:

thisExp.addData(“MovDur”, stim_dur)