How to use "random" in processing to choose a color
Chapter7: Input/output Technology
Section: Chapter Questions
Problem 4VE
Related questions
Question
How to use "random" in processing to choose a color
Expert Solution
Step 1
random function:
- random() Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If only one parameter is passed to the function, it will return a float between zero and the value of the high parameter.
- To generate random colors, we randomly mix varying amounts of red, blue, and green together. There are 256 different amount options for each color. That means that when we generate a random color, it could be one of 256^3 = 16,777,216 colors.
Example 1 of using random() in processing:
void setup() {
size(200, 100);
noSmooth();
}
void draw() {
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
float r = random(256);
float g = random(256);
float b = random(256);
stroke(r, g, b);
point(x, y);
}
}
}
This code uses a nested for loop to loop over every pixel in the window. For each pixel, it creates a random color and draws a point with that color at that pixel.
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning