Random Colors

Random Colors

September 24, 2016

example processing for-loop
void setup() {
  size(200, 100);

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

Code Editor

See the Pen by Happy Coding (@KevinWorkman) on CodePen.

Tweak Ideas

  • Instead of drawing a random color, use the noise() function to come up with a shade of gray for each pixel. Read more about Perlin noise for more info.
  • Take a big number like pi or Euler’s number. Visualize that number by going through the first 100 (or 1000, or 1,000,000) digits. For each digit, draw a pixel with a color based on that digit. For example if the digit is 0 draw a red pixel, if it’s 1 draw a green pixel, etc.

For Loops Examples


Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!