# Randomize duration of a Routine in Builder

**URL:** <https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436>\
**Category:** Builder\
**Created:** [March 18, 2021, 6:51am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436 "2021-03-18T06:51:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 18, 2021, 6:51am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/1 "2021-03-18T06:51:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 18, 2021, 9:03am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/2 "2021-03-18T09:03:45Z")

</div>

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

```auto
fixDur = 0

```

In the **Begin Routine tab** enter the following code

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

```

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 9:09am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/3 "2021-03-19T09:09:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 19, 2021, 9:18am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/4 "2021-03-19T09:18:38Z")

</div>

The dollar sign should not appear in code components

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 9:25am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/5 "2021-03-19T09:25:38Z")

</div>

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

 ![PrtScr capture_418](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/7/c78b63eba78d8847b749da664e8e6db23fbd5d3a.jpeg)

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 19, 2021, 9:32am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/6 "2021-03-19T09:32:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 9:43am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/7 "2021-03-19T09:43:11Z")

</div>

No, a more recent one: PsychoPy 2021.1.2

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 19, 2021, 9:48am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/8 "2021-03-19T09:48:07Z")

</div>

Hello,

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

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 9:56am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/9 "2021-03-19T09:56:51Z")

</div>

Error:

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

```

SyntaxError: invalid syntax

##### Experiment ended.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 19, 2021, 10:01am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/10 "2021-03-19T10:01:44Z")

</div>

Hello,

does this [RandFix.psyexp](https://discourse.psychopy.org/uploads/short-url/5C3AtlF2ObKf8Igb83Wei4J3nZn.psyexp) (13.5 KB) run?

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 10:09am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/11 "2021-03-19T10:09:16Z")

</div>

Yes, it looks like this:

 ![PsychoPy_2](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/c/cc8257eba49935e901a481a4ab8db3cb0f47ff32.jpeg)

And gradually counts down/

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 19, 2021, 10:24am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/12 "2021-03-19T10:24:31Z")

</div>

Hello,

looks good. It is not a count-down. 😉 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

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 19, 2021, 5:58pm UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/13 "2021-03-19T17:58:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 19, 2021, 6:38pm UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/14 "2021-03-19T18:38:16Z")

</div>

@JensBoelte means in terms of numbers of routines.

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

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 21, 2021, 9:57am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/15 "2021-03-21T09:57:01Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 21, 2021, 10:23am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/16 "2021-03-21T10:23:40Z")

</div>

Have you tried without the dollar sign?

---

<div class="post-metadata">

**Author:** ![Ekaterina\_Bono](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ekaterina_bono/32/14504_2.png) [@Ekaterina\_Bono](https://discourse.psychopy.org/u/Ekaterina_Bono)\
**Post date:** [March 21, 2021, 12:26pm UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/17 "2021-03-21T12:26:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [July 2, 2022, 7:33pm UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/18 "2022-07-02T19:33:14Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/0/f/0f6da1e92f8433c65e44a20171512b587824683d.png)

Any ideas?

Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [October 23, 2022, 6:47am UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/19 "2022-10-23T06:47:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ange](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@Ange](https://discourse.psychopy.org/u/Ange)\
**Post date:** [October 28, 2022, 6:01pm UTC](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436/20 "2022-10-28T18:01:59Z")

</div>

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)

[Next page](https://discourse.psychopy.org/t/randomize-duration-of-a-routine-in-builder/21436.md?page=2)
