// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 4-7: Filling variables with random values
float r;
float g;
float b;
float a;
float diam;
float x;
float y;
void setup() {
size(200,200);
background(255);
smooth();
}
void draw() {
// Each time through draw(), new random numbers are picked for a new ellipse.
r = random(255);
g = random(255);
b = random(255);
a = random(255);
diam = random(20);
x = random(width);
y = random(height);
// Use values to draw an ellipse
noStroke();
fill(r,g,b,a);
ellipse(x,y,diam,diam);
}
When discussing the usage of the random() function in your book, you explained the need to sometimes change the value received from random() into an integer using int() or casting it as (int).
Granted it is not necessary to always do this, but I think it would have been much wiser to do it in this example considering RGB values should really not be floats and doing so would have added a level of consistency.
Comment by haptiK — December 26, 2009 @ 12:15 pm
Thanks for the suggestion. Although color values (as well as pixel values) are truly integers at heart, Processing allows you to use floats with all of its functions for convenience. So I find that it’s actually often more confusing to have to write the extra code:
int r;
r = int(random(255));
But this is an important point that I should be aware of, thank you!!
Comment by Daniel Shiffman — January 4, 2010 @ 12:52 pm