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.
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.
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.
Most helpful comment
Hello @MDeter67,
Thank you for opening this issue.
randomfunction outside p5 functions (i.e beforesetup())?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
randomwithin a p5 function.So the following code won't work (throwing the error you reported):
But this should work:
Hope it helps, feel free to add any additional info if my answer isn't helping.