for-Loop w/ Array of random RGB values

#1

Hi! I’m using Casey and Ben’s Getting Stared with Processing book. Example 11-8, pp 155-156, shows how to fill an ARRAY w RANDOM values using a FOR loop. The RANDOM values are passed to STROKE values. The example below is in Gray.

I thought it would be very straight forward to change the STROKE values from random Gray, to random RGB…I was wrong–it’s IMPOSSIBLE!!! haha. :upside_down_face: Could someone please help with advice on changing stroke values to random RGB.

Thanks!

float[] rainbow;

void setup() {
  colorMode(RGB);
  size(600, 600);
  rainbow = new float[width];
  for(int i = 0; i < rainbow.length; i++) {
    rainbow[i] = random(0, 255);
  }
}

void draw() {
  for(int i=0; i < rainbow.length; i++ ) {
    stroke(rainbow[i]);
    line(i, 0, i, height);
  }
}
1 Like
#2



color[] rainbow;

void setup() {
  // colorMode(RGB);
  size(600, 600);
  rainbow = new color[width];
  for (int i = 0; i < rainbow.length; i++) {
    rainbow[i] = color(random(0, 255), random(0, 255), random(0, 255));
  }
}

void draw() {
  for (int i=0; i < rainbow.length; i++ ) {
    stroke(rainbow[i]);
    line(i, 0, 
      i, height);
  }
}
3 Likes
#3

oh my gosh… THANK YOU!!!

1 Like