| Reference | Downloads | Github

Time randomization for a stimulus in a routine


We are trying to do an experiment in which a stimulus (a text,always the same) is presented for a random time and then it goes to the next step. When the first trial ends, it goes back to the first routine in the loop and then the same stimulus(the text) is presented for a random time. Everything is already done but I haven’t been able to randomize the time of the stimulus in the loop.
Any help will be highly appreciated!

Hello. Please specify what you mean by “random”.

1 Like

Thank you for reading my question!

For example, in trial1, the text will appear for 120(s) and the next trial is randomly appeared for 300(s).
We want the text to appear for a random duration in the interval of [2(m),5(m)]. so in one trial it’s 4 minutes and the next trial it’s 2 minutes but we want the program to randomly pick those durations.

Thank you again for any help you might be able to provide!

I just coded in the randomization! Thank you very much!

1 Like


I was looking to randomize the timing exactly as you did, but also got stuck in the same way.

May I please ask how you did it?



In the routine that you want it to appear for a random amount if time, make a coding component and in the Begin Routine of the component type in:

import random 
x= random.randint(5,10)

It will appear for a random time between 5 seconds and 10 seconds.

Hope this helps!

1 Like

Just to clarify, Builder files all import various random functions from the numpy.random library:

from numpy.random import random, randint, normal, shuffle

so there is no need to import random from the Python standard library (and this could actually cause some conflicts). So you have direct access to a randint function like this:

x = randint(low = 5, high = 11) # values will range from 5 to 10

You might also want to store that value in the data, as it won’t happen automatically:

thisExp.addData('random_duration', x)

May i know how to do time randomization in the builder view?

Thanks in advance!

The above discussion is for the Builder view. As @Sarvenaz_Ghattan stated above, you want to insert a code component (from the “custom” component panel).

That way you can define a variable that can then be used in other stimulus components. You could put an expression like random() directly into a field in a stimulus component, but then you wouldn’t also be able to save it in your data.

NB the code component needs to appear above your stimulus components, so that they get to refer to the current value of any variables you create.

We can’t give any more specific guidance unless you fully describe what kind of randomisation you want to achieve.

Hi there, how would the expression be written directly into the field in the stimulus component if I want the stimulus to be presented randomly between 2-4 seconds?

Alternatively, how would I write the code in the code component?

I have tried with:

x = randint(low = 2, high = 4)

but I don’t know how to refer to a specific stimulus. What does “x” stand for in that code?

This is just the name of a variable where you want to store the result, so it can be used later, such as for recording in your data file, and using in a stimulus component. So don’t call it x, but instead give it a name that has meaning to you, e.g. stimulus_interval or image_start or whatever makes sense for your needs:

stimulus_interval = randint(low = 2, high = 4) # choose a value
thisExp.addData('stimulus_interval', stimulus_interval) # record it in the data file

then you can use the name $stimulus_interval in your stimulus component fields, e.g. the “start” time field of an image stimulus.

1 Like

That was really helpful, thank you!