Random duration every trial for a routine in a loop

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
I have a loop composed of two images
I want the first image to appear on the screen for a random duration between 5 and 10 seconds. I want this random time to be newly selected for each trial (I have 5 reps for now in my loop).

What did you try to make it work?:
I have already tried to set randint(5,10) within the duration box in the builder.
I also tried to do something similar in the code:
stimulus_interval = randint(5,10)
routineTimer.add(stimulus_interval)

In those cases I got the random value selected between 5 and 10 (i.e. 8 sec) but it is always the same for each trial (so that in each trial the duration of my routine is fixed to 8 secs)

Iā€™ve also tried with the custom (Begin Routine) by inserting:
import random
Jitter=[randint(5,10)]
(in this case I got this error message:
TypeError: unsupported operand type(s) for +: ā€˜floatā€™ and ā€˜listā€™

Experiment ended.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. ā€œThat didnā€™t workā€ is not enough information.

When I do this I set the duration for all the components as e.g. stimulusTime and then set this in the Begin Routine code block (on in a previous preparation routine)

1 Like

Thank you very much wakecarter!
I managed to code it with:

in the ā€œBegin experimentā€ tab of a code component,

#create a list with 6 elements:
duration_list = [5, 6, 7, 8, 9, 10]
#randomise its order:
shuffle(duration_list)

in ā€œBegin Routineā€ :

trial_duration = duration_list.pop()