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
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!
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:
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.
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
thank you in advance!
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).
Hello,
I have a similar problem. I want to randomize the time only for 5 stimuli present on a list of 40 stimuli. The majority of the stimuli are presented for 1 second, but the other 5 stimuli must be presented between 4 and 5 seconds.
I don’t know if I’m clear enough. It’s not the time of the whole routine that I want to randomize, nor the whole list of stimuli, but only 5 stimuli whose appearance is randomized, and for which the appearance time must be between 4 and 5 seconds, while all other stimuli in the Excel list must be presented for 1 second
do you want presentation durations of 4 to 5 seconds for specific stimuli or just any stimuli? Any easy solution would be to specific the presentation duration in a column in Excel. But in this case, your five stimuli would always use the same presentation duration across participants.