How to randomly change the background color between 'X' amount of static colors with each output

I’ve been trying this for a couple of days and can never seem to get it right with what I imagine would be a simple piece of syntax/code.

Each generated image’s background I want to either be between -1 to 256, and also 3 other specific RGB colors. So far, I’ve just got this:

void setup() {
  size(500, 500);
background(random(-1, 256));

The above works fine for grayscale & W/B, but what I want to do was something that I imagined would look like this:

background(random(-1, 256)(#DC1A1A)(#1AA9DC)(#1ADC34));

Or maybe as simple as this:

background(random(-1, 256, #DC1A1A, #1AA9DC, #1ADC34));

But of course, neither of those work. I’ve looked through Reference and through topics, but I still can’t find what I imagine to be a simple line of code. Can someone please exactly tell me what it would be?

Note: This isn’t my homework :slight_smile:

You could create a color palette array and use random() to pick an index within its length range:

// between-x-amount-of-static-colors-with-each-output/33880/2

// GoToLoop [2021-Dec-02]

static final color[] PALETTE = {
  0, // Black [0]
  -1, // White [1]
  #DC1A1A, // Vivid Red [2]
  #1AA9DC, // Vivid Blue [3]
  #1ADC34, // Vivid Lime Green [4]

void setup() {
  textAlign(CENTER, CENTER);

void draw() {
  final int idx = (int) random(PALETTE.length);
  final color c = PALETTE[idx];
  text(idx, width >> 1, height >> 2);
  text(hex(c, 6), width >> 1, height >> 1);
1 Like

Thanks, I have seen that page already but I still don’t know how to format that with ‘background(…)’.

The length range I’ve done with -1, 256, but I’m still not getting how I format this for background with 3 other options. Thanks for the response.