# Problem with using random numbers in stimulus timings

**URL:** <https://discourse.psychopy.org/t/problem-with-using-random-numbers-in-stimulus-timings/21379>\
**Category:** Builder\
**Created:** [March 16, 2021, 4:26pm UTC](https://discourse.psychopy.org/t/problem-with-using-random-numbers-in-stimulus-timings/21379 "2021-03-16T16:26:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Buehner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marc_buehner/32/3476_2.png) [@Marc\_Buehner](https://discourse.psychopy.org/u/Marc_Buehner)\
**Post date:** [March 16, 2021, 4:26pm UTC](https://discourse.psychopy.org/t/problem-with-using-random-numbers-in-stimulus-timings/21379/1 "2021-03-16T16:26:14Z")

</div>

**OS** (e.g. Win10): MAc OS 11.2.3  
**PsychoPy version** (e.g. 1.84.x): 2021.1.2  
**Standard Standalone? (y/n)** y  
\*\*What are you trying to achieve?:

I had an experiment in builder and on Pavlovia that I created last year, but it broke after updating to latest version. Even though I tried using older version, I cannot get it to run. So I re-created it from scratch, but now I get different errors.  
Specifically, I need to display a stimulus for a random interval between 1200 and 1800ms

**What did you try to make it work?:**

I have the text stimulus and set its duration to $NC\_Wait  
I have a code component  
`NC_Wait =np.random.uniform(1.2,1.8)`  
I get this error message:  
if tThisFlip \> asarray($NC\_Wait)-frameTolerance:  
^  
SyntaxError: invalid syntax

I tried taking away the $, using just random without np. and also leaving out the uniform.

Any ideas?

---

<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:29am UTC](https://discourse.psychopy.org/t/problem-with-using-random-numbers-in-stimulus-timings/21379/2 "2021-03-18T09:29:24Z")

</div>

Hello,

the following psyexp-file shows how to implement a random stimulus duration.

[RandFix.psyexp](https://discourse.psychopy.org/uploads/short-url/tQRDY7ExaWXm6UuwURi3Ir8iSxg.psyexp) (6.5 KB)

I do not really see the need for a uniform distribution when you generate a random number for each trial. Anyway, if you want to use a uniform random number generation, change the line

```auto
stim_dur = 1.2+(1.8-1.2)*random()

```

to

```auto
stim_dur = np.random.uniform(1.2,1.8)

```

Notice, that random.uniform uses a half-open interval [low, high). The lower boundary is included, the upper boundary is excluded.

Best wishes Jens
