 # Time randomization for a stimulus in a routine

Hello,

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!
Thanks

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

Hi,

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

Thanks!

Hello,

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!
Sarvenaz

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)``
1 Like

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

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.

2 Likes

That was really helpful, thank you!

Hi all,
I have done exactly what you suggested: I want to have a fixation of random time between 500 ms and 1 second, so I added above the fixation a code custom component, in the Begin routine:

``````time = randint(0.5,1.0)
``````

End of routine:

``````thisExp.addData('random_duration', time)
``````

Then, in the fixation component, duration box I specified `\$time`.
However, I can’t see at all the fixation appearing on the screen. Any suggestion???
My version of Psychopy is 2020.1.3

`randint()` is a function that generates random integers, so won’t do what you want. You also don’t want the standard `random()`, as although that samples from a continuous distribution, stimulus durations are discrete multiples of the screen refresh rate. Screens typically run at 60 Hz. If so, you might want to sample from a list of durations that are multiples of 16.6666 ms (e.g. 0.5, 0.55, 0.6 s, etc).

Dear Michael,
Could the subsequent lines of code be a solution for my problem?

“Begin experiment”= code component:

``````duration_list = [0.5, 0.55, 0.6, 0.65, 0.70, 0.80, 0.85, 0.9, 0.95, 1.0]
shuffle(duration_list)
``````

“Begin routine” = code component:

``````trial_duration = duration_list.pop()
``````

“End routine” = code component:

``````loop.addData('trial_duration', trial_duration)
``````

Or would you have a simpler solution/advice?

Thank you very much!

Sorry for the delay. This looks good, but I’m guessing you’ve tried it and verified that already by now.