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

**URL:** <https://discourse.processing.org/t/how-to-randomly-change-the-background-color-between-x-amount-of-static-colors-with-each-output/33880>\
**Category:** Beginners\
**Created:** [December 2, 2021, 9:45am UTC](https://discourse.processing.org/t/how-to-randomly-change-the-background-color-between-x-amount-of-static-colors-with-each-output/33880 "2021-12-02T09:45:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sloppy](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@sloppy](https://discourse.processing.org/u/sloppy)\
**Post date:** [December 2, 2021, 9:45am UTC](https://discourse.processing.org/t/how-to-randomly-change-the-background-color-between-x-amount-of-static-colors-with-each-output/33880/1 "2021-12-02T09:45:36Z")

</div>

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:

```auto
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:

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

```

Or maybe as simple as this:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 2, 2021, 11:22am UTC](https://discourse.processing.org/t/how-to-randomly-change-the-background-color-between-x-amount-of-static-colors-with-each-output/33880/2 "2021-12-02T11:22:52Z")

</div>

> [@sloppy](#):
>
> , but I still can’t find what I imagine to be a simple line of code.

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

> **[random() / Reference](https://processing.org/reference/random_.html)**
>
> 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…

```auto
// Discourse.Processing.org/t/how-to-randomly-change-the-background-color-
// 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() {
  frameRate(1);
  fill(0200);
  textSize(030);
  textAlign(CENTER, CENTER);
  println(PALETTE);
}

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

```

---

<div class="post-metadata">

**Author:** ![sloppy](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@sloppy](https://discourse.processing.org/u/sloppy)\
**Post date:** [December 2, 2021, 4:16pm UTC](https://discourse.processing.org/t/how-to-randomly-change-the-background-color-between-x-amount-of-static-colors-with-each-output/33880/3 "2021-12-02T16:16:21Z")

</div>

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.
