# Random() vs. Math.random() in p5js

**URL:** <https://discourse.processing.org/t/random-vs-math-random-in-p5js/39660>\
**Category:** Beginners\
**Created:** [November 10, 2022, 2:44pm UTC](https://discourse.processing.org/t/random-vs-math-random-in-p5js/39660 "2022-11-10T14:44:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkotty](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@rkotty](https://discourse.processing.org/u/rkotty)\
**Post date:** [November 10, 2022, 2:44pm UTC](https://discourse.processing.org/t/random-vs-math-random-in-p5js/39660/1 "2022-11-10T14:44:10Z")

</div>

I wanted to create global random variables, but when I used random() outside of all the functions, random() was not recognized.  
A student Googled that I needed to use Math.random() in this case. Why is that?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 10, 2022, 3:07pm UTC](https://discourse.processing.org/t/random-vs-math-random-in-p5js/39660/2 "2022-11-10T15:07:20Z")

</div>

Hi @rkotty,

Welcome to the forum! 😉

Interesting remark! If you run it in the p5js web editor, you get the following error messages:

> 🌸 p5.js says:  
> [sketch.js, line 1] “random” is not defined in the current scope. If you have defined it in your code, you should check its scope, spelling, and letter-casing (JavaScript is case-sensitive).
> 
> - More info: [examples | p5.js](https://p5js.org/examples/data-variable-scope.html)  
> Did you just try to use p5.js’s random() function? If so, you may want to move it into your sketch’s setup() function.
> 
> - More info: [why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup](https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup)

And the linked wiki page explains it very well:

> In regular global mode, p5 variable and function names are not available outside `setup()`, `draw()`, `mousePressed()`, etc. (Except in the case where they are placed inside functions that are called by one of these methods.)

This works:

```javascript
let randomValue;

function setup() {
  createCanvas(400, 400);
  randomValue = random(1, 100);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 10, 2022, 3:24pm UTC](https://discourse.processing.org/t/random-vs-math-random-in-p5js/39660/3 "2022-11-10T15:24:52Z")

</div>

P5.js library automatically dumps its API in the global context only after it finds either callbacks **setup()** or **draw()** are present in the global context as well.

Therefore any code outside functions runs too early before that checking.

If you really need to access p5js properties earlier you can prematurely instantiate it via `new p5;` as this sketch below does for **color()**, **cos()**, **sin()** and DEG\_TO\_RAD:

> **[Sierpinski Pentagon \[p5.js\] - GoToLoop - OpenProcessing](https://openprocessing.org/sketch/955445)**
