P5.js: Random is not defined

Created on 31 Aug 2020  路  2Comments  路  Source: processing/p5.js

Most appropriate sub-area of p5.js?

  • [ ] Accessibility (Web Accessibility)
  • [let r = random (0,180;)] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] Dom
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Other (specify if possible)

Details about the bug:

I am using macOS Catalina 10.15 with Microsoft Edge Version 85.0.564.41. When I try to run random in an argument such as:

let r = random(0,180);

the computer prints out "random was not defined" at the end of the statement.

bug

Most helpful comment

Hello @MDeter67,

Thank you for opening this issue.

  • Do you have any minimal code example that would allow us to reproduce the problem?
  • Are you trying to use the random function outside p5 functions (i.e before setup())?

If yes, I would suggest that you have a look at this article from the wiki.
This section is also worth reading, especially if you want to use p5 with other JavaScript libraries.

By default, you need to use a function such as random within a p5 function.

So the following code won't work (throwing the error you reported):

let r = random(0, 180); // random isn't accessible outside p5's functions

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

function draw() {
  background(r);
}

But this should work:

let r;

function setup() {
  r = random(0, 180);
  createCanvas(400, 400);
}

function draw() {
  background(r);
}

Hope it helps, feel free to add any additional info if my answer isn't helping.

All 2 comments

Welcome! 馃憢 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

Hello @MDeter67,

Thank you for opening this issue.

  • Do you have any minimal code example that would allow us to reproduce the problem?
  • Are you trying to use the random function outside p5 functions (i.e before setup())?

If yes, I would suggest that you have a look at this article from the wiki.
This section is also worth reading, especially if you want to use p5 with other JavaScript libraries.

By default, you need to use a function such as random within a p5 function.

So the following code won't work (throwing the error you reported):

let r = random(0, 180); // random isn't accessible outside p5's functions

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

function draw() {
  background(r);
}

But this should work:

let r;

function setup() {
  r = random(0, 180);
  createCanvas(400, 400);
}

function draw() {
  background(r);
}

Hope it helps, feel free to add any additional info if my answer isn't helping.

Was this page helpful?
0 / 5 - 0 ratings