# Random to initialize variables

**URL:** <https://discourse.processing.org/t/random-to-initialize-variables/21466>\
**Category:** Beginners\
**Created:** [May 31, 2020, 11:39pm UTC](https://discourse.processing.org/t/random-to-initialize-variables/21466 "2020-05-31T23:39:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidhurwich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidhurwich/32/9458_2.png) [@davidhurwich](https://discourse.processing.org/u/davidhurwich)\
**Post date:** [May 31, 2020, 11:39pm UTC](https://discourse.processing.org/t/random-to-initialize-variables/21466/1 "2020-05-31T23:39:51Z")

</div>

I have a question. I’ve noticed that when I initialize variables with a value before setup(); or draw(); that it works for a string, boolean, number.

However, if I try to set that value to a random number using random() it will not work outside of setup(); or draw(). Why is it that functions do not work outside of setup() or draw()?

Any information would be appreciated.

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [June 1, 2020, 5:24am UTC](https://discourse.processing.org/t/random-to-initialize-variables/21466/2 "2020-06-01T05:24:43Z")

</div>

Welcome to the forum, @davidhurwich!

Technically, p5.js is not ready when that variable is initialized. So `random()` is not available to be called. If you take a look at the JavaScript console in your browser’s developer tools, you’ll probably see an error like `ReferenceError: Can't find variable: random`.

You could get a random number by using vanilla JavaScript instead: [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random). Or use this workaround:

```javascript
new p5();

var boop = random(100);

function setup() {
    createCanvas(100, 100);
}

function draw() {
    background(255, 0, boop);
}

```

Source: [Why can’t 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)

---

<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:** [June 1, 2020, 10:46am UTC](https://discourse.processing.org/t/random-to-initialize-variables/21466/3 "2020-06-01T10:46:08Z")

</div>

> [@davidhurwich](#):
>
> Why is it that functions do not work outside of **setup()** or **draw()**?

> [@When can you use cos and sin outside of setup or draw](http://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/4):
>
> I’ve changed your post to the p5.js forum category then. expressionless p5js is a diff. flavor beast. bear Indeed, none of its API is available until either it finds out that setup() or draw() is in the global context (window) or we manually instantiate class p5: new p5; cowboy_hat_face new p5; // prematurely instantiate class p5. ;-) const SQRT\_3 = sqrt(3); // we can access p5's sqrt() method outside functions! function setup() { print(SQRT\_3); } For cos() & sin() and similar, we …
