How to use "random" in processing to choose a color

Programming with Microsoft Visual Basic 2017
8th Edition
ISBN:9781337102124
Author:Diane Zak
Publisher:Diane Zak
ChapterC: Finding And Fixing Program Errors
Section: Chapter Questions
Problem 3RQ
icon
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.

random colors

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Rendering
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.
Similar questions
Recommended textbooks for you
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
CMPTR
CMPTR
Computer Science
ISBN:
9781337681872
Author:
PINARD
Publisher:
Cengage
Enhanced Discovering Computers 2017 (Shelly Cashm…
Enhanced Discovering Computers 2017 (Shelly Cashm…
Computer Science
ISBN:
9781305657458
Author:
Misty E. Vermaat, Susan L. Sebok, Steven M. Freund, Mark Frydenberg, Jennifer T. Campbell
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr