# Math.random() function in PsychoPy for Pavlovia

**URL:** <https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942>\
**Category:** Coding\
**Created:** [November 9, 2020, 10:53am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942 "2020-11-09T10:53:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcusGlennon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marcusglennon/32/11677_2.png) [@MarcusGlennon](https://discourse.psychopy.org/u/MarcusGlennon)\
**Post date:** [November 9, 2020, 10:53am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/1 "2020-11-09T10:53:17Z")

</div>

Hello,

I am attempting to use a random function to determine feedback values, but I am having some trouble.

As I have learnt, it is impossible to use “import” functions in Pavlovia, so I am having to find a way around using “import random”.

However, I have fallen at the first hurdle; if I simply use the Math.random() function in a JS code component, my experiment does not work at all. I simply typed “x = Math.random()” and I put “$x” in a text component (set every frame) in the same routine. The experiment then crashes when I run it.

I am sure I am just making a very simple mistake somewhere - can anyone point me in the right direction?

Thanks!  
Marcus

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 9, 2020, 11:05am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/2 "2020-11-09T11:05:16Z")

</div>

Hi Marcus,

Sounds like you are on the right track, please could you let us know what error message you get when your experiment crashes and, if possible, share a miminaly working version of your file/problem 🙂

As a first check - do you have x specified in both python and JS? if not then it will crash locally because x isn’t defined. Try using a code component where the code type is ‘both’ and on the left you have `x =np.random.rand()` and on the right` x = Math.random()`

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![MarcusGlennon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marcusglennon/32/11677_2.png) [@MarcusGlennon](https://discourse.psychopy.org/u/MarcusGlennon)\
**Post date:** [November 9, 2020, 11:33am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/3 "2020-11-09T11:33:00Z")

</div>

Hi Becca,

Thank you for this, it has worked! I should be able to extrapolate this method and use it in my broader experiment now.

Just out of curiosity though, why can x not just be defined in a JS code component? And what exactly is the “np.random.rand()” function doing?

Thanks,  
Marcus

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 9, 2020, 11:42am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/4 "2020-11-09T11:42:45Z")

</div>

Hmm actually sorry I think you might want random.random() on your python side ( I assumed you wanted to randomly sample a number between 0 and 1 since this is what Math.random does [https://stackoverflow.com/questions/33359740/random-number-between-0-and-1-in-python](https://stackoverflow.com/questions/33359740/random-number-between-0-and-1-in-python)) - random.random() and math.random() should be functionally equivalent.

In response you your Q _‘why can x not just be defined in a JS code component?’_ - technically it can, but that code will only be run online. Any JS code components will be run online and any Py components will be run locally.

Locally you rexperiment uses PsychoPy, so only interprets python code. Online, you expeirment used PsychoJS so will monly read JS components.

Hope this helps 🙂

If the solution worked please could you also mark the solution for future users!

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![MarcusGlennon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marcusglennon/32/11677_2.png) [@MarcusGlennon](https://discourse.psychopy.org/u/MarcusGlennon)\
**Post date:** [November 9, 2020, 11:57am UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/5 "2020-11-09T11:57:49Z")

</div>

Great. The random.random() function works just as well, providing I add “import random” too. What is the “random.random()” function doing differently to “np.random.rand()”?

Brilliant, that is incredibly helpful, thank you very much!  
Marcus

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 9, 2020, 12:01pm UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/6 "2020-11-09T12:01:26Z")

</div>

technically I think they output the same thing but np.random.random() can take additional parameters and has more methods for generating a uniform distribution over a range a useful thread might be this here [https://stackoverflow.com/questions/7029993/differences-between-numpy-random-and-random-random-in-python](https://stackoverflow.com/questions/7029993/differences-between-numpy-random-and-random-random-in-python) 🙂 as well as the official documentation on these libraries

> **[random — Generate pseudo-random numbers — Python 3.9.0 documentation](https://docs.python.org/3/library/random.html)**

  

> **[numpy.random.random — NumPy v1.15 Manual](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.random.html)**

Becca

---

<div class="post-metadata">

**Author:** ![kaylap](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kaylap/32/16665_2.png) [@kaylap](https://discourse.psychopy.org/u/kaylap)\
**Post date:** [November 23, 2021, 9:14pm UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/7 "2021-11-23T21:14:22Z")

</div>

I tried using this fix on my local Python code components, and it’s not working. I tried substituting random.random() and math.random() for np.random(), neither of which worked. With random.random(), I get `AttributeError: 'builtin_function_or_method' object has no attribute 'random'`  
and with math.random() I get `NameError: name 'math' is not defined`.

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 24, 2021, 5:41pm UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/8 "2021-11-24T17:41:42Z")

</div>

Hi There,

In the newest release this should no longer be needed. You should be able to use `random()` and that will work both locally and online.

Thanks,  
Becca

---

<div class="post-metadata">

**Author:** ![kaylap](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kaylap/32/16665_2.png) [@kaylap](https://discourse.psychopy.org/u/kaylap)\
**Post date:** [November 24, 2021, 6:07pm UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/9 "2021-11-24T18:07:33Z")

</div>

Is there a version of random.uniform() that works? I tried both that and just random(), but with random() I get the following error:  
`TypeError: random() takes at most 1 positional argument (2 given)`

For context, I’m trying to generate a random integer between two values:  
`lower_t1_x = random(-785, 445)`

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 24, 2021, 10:42pm UTC](https://discourse.psychopy.org/t/math-random-function-in-psychopy-for-pavlovia/17942/10 "2021-11-24T22:42:24Z")

</div>

locally `random.uniform` will use the numpy library, which will not work online because it is python but it looks like the JS version of random samples over an approximately uniform distribution, but between 0 and 1. Could you try using linspace instead?

Add a code component, set type to JS, use this in the before experiment tab:

```auto
//provide the JS alternative to any functions that exist in python but not JS
function linspace(startValue, stopValue, cardinality) {
  var arr = [];
  var step = (stopValue - startValue) / (cardinality - 1);
  for (var i = 0; i < cardinality; i++) {
    arr.push(startValue + (step * i));
  }
  return arr;
}

```

then you can call it later using `linspace(start, stop, cardinality)`

Becca
