We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Is there a way to use increments using the random function??
For example, x = random(0,50) will give me a number between 0 and 50. What if I want it to give me either 0 or 50, but not the numbers in between??
void setup() { frameRate(1); } void draw() { int x = random(1) < .5? 0 : 50; print(x, TAB); }
I'm pretty new to programing, could you explain what this means?
https://Processing.org/reference/conditional.html https://Processing.org/reference/random_.html https://Processing.org/reference/frameRate_.html
If random (number between 0 and 1) is smaller than 0.5 then set x to 0 : otherwise set x to 50.
This:
int x = random(1) < .5? 0 : 50;
Can also be written like this:
if (random(1.0) < 0.5) x = 0.0; else x = 50.0;
The random(1.0) will produce a number between 0.0 and 1.0 and there is about a 50% chance it will be less than 0.5. If it is less than 0.5 then x is assigned 0.0, otherwise it is assigned 50.0
@asimes, you've forgotten to declare variable x! :-\"
int x = random(1) < .5? 0 : 50; print(x, TAB);
is equivalent to:
int x; if (random(1) < .5) x = 0; else x = 50; print(x, TAB);
Answers
I'm pretty new to programing, could you explain what this means?
https://Processing.org/reference/conditional.html
https://Processing.org/reference/random_.html
https://Processing.org/reference/frameRate_.html
If random (number between 0 and 1) is smaller than 0.5 then set x to 0 : otherwise set x to 50.
This:
Can also be written like this:
The random(1.0) will produce a number between 0.0 and 1.0 and there is about a 50% chance it will be less than 0.5. If it is less than 0.5 then x is assigned 0.0, otherwise it is assigned 50.0
@asimes, you've forgotten to declare variable x! :-\"
is equivalent to: