# What is the code to generate a random and automatic color in the background?

**URL:** <https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [April 21, 2021, 3:17pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544 "2021-04-21T15:17:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 21, 2021, 3:17pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/1 "2021-04-21T15:17:46Z")

</div>

Hello, I’m new to processing, and I would like to know if anyone can help me solve a question about how I change my background with random colors. That is, every time I clicked on play and became active, the color would change randomly and automatically.  
Thank you

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 21, 2021, 3:28pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/2 "2021-04-21T15:28:31Z")

</div>

Hi! Welcome to the forum!

Is this a homework assignment? If so please add a tag to the topic. Or are you self learning? Either way, it’s better if you try something, post the code and we give you feedback. Do you have any idea where to begin with?

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 21, 2021, 3:48pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/3 "2021-04-21T15:48:39Z")

</div>

Yes it is a school exercise. Oh ok sorry, it was the first time I asked a question here. Yes, I already have this, I don’t know if it’s right or not, I’m still very amateur in this:

float r;  
float g;  
float b;  
float a;  
float diam;  
float x;  
float y;

void setup() {  
size(2000,2000);  
background(0);  
smooth();  
}

void draw() {  
r = random(255);  
g = random(255);  
b = random(255);  
a = random(255);  
diam = random(20);  
x = random(width);  
y = random(height);  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 21, 2021, 3:53pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/4 "2021-04-21T15:53:09Z")

</div>

> [@Marga](#):
>
> size(2000,2000);

Wow that’s a big screen. I mostly go for 1200, 800

**background**

use background(r,g,b); in draw()

problem: color changes throughout

you could make a function after the last } :

```auto
void mousePressed() {
r = random(255);
g = random(255);
b = random(255);
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 21, 2021, 4:36pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/5 "2021-04-21T16:36:28Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

There are lot of resources (tutorials, references, examples, etc.) here:  
_[processing.org](http://processing.org)_

You want a random number between 0 and 255 ( this is the default colorMode() )

`random(256);`  
will give you a random float between 0 and 256 but _not including 256_.  
You can also convert that to an int.

This is explained in the references.

Some references:

- [colorMode() \ Language (API) \ Processing 3+](https://processing.org/reference/colorMode_.html)
- [color \ Language (API) \ Processing 3+](https://processing.org/reference/color_datatype.html)
- [random() \ Language (API) \ Processing 3+](https://processing.org/reference/random_.html)

You can slow things down also with frameRate() if you want to get a sense of what your code is doing each cycle of draw().

Suggesting reading:

- [setup() \ Language (API) \ Processing 3+](https://processing.org/reference/setup_.html)
- [draw() \ Language (API) \ Processing 3+](https://processing.org/reference/draw_.html)  
And the tutorials as well.

`:)`

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 21, 2021, 5:51pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/6 "2021-04-21T17:51:00Z")

</div>

Okay, thank you very much for your help. I will try!

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 21, 2021, 6:22pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/7 "2021-04-21T18:22:40Z")

</div>

Okay, thank you for your help and for the advices. I will try! 🙂

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 28, 2021, 2:17pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/8 "2021-04-28T14:17:47Z")

</div>

Good afternoon,  
Following my initial doubt, I tried to change the code in order to solve my problem, however, I only managed to get the colors to change between black, gray and white and not for more colors. Can someone help me solve the rest of my question? Thank you very much and here’s what I got so far:

```auto
float r;
float g;
float b;

void setup() {
 size(1000,1000);
 background(random(255));
 smooth();
 frameRate(1);
}  

void draw() {
  background(random(255)); 
 r = random(RGB, 255);
 g = random(RGB, 255);
 b = random(RGB, 255);
 
 }

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 28, 2021, 2:40pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/9 "2021-04-28T14:40:44Z")

</div>

There are resources here:  
_[https://processing.org/](https://processing.org/)_

There are tutorials, references and examples.

You should be looking at the reference for every method you are using and the related references.

`:)`

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 28, 2021, 2:46pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/10 "2021-04-28T14:46:04Z")

</div>

Hello,

Your background is always grey because of this line:

```auto
background(random(255));

```

If you take the reference of the function (that you can find [here](https://processing.org/reference/background_.html)) you are actually using the following overload: `background(gray)`

What you do after with your `r`, `g` and `b` variable doesn’t do anything because you are not using them. Try removing those 3 lines and you’ll see your code is working exactly the same way.

It is not because you are using the RGB color mode that setting 3 r, g and b variables are enough to change the color. Actually you can use any name that you want as long as you feed the `background()` function with the proper value.

Also, you want to check the [random() function reference](https://processing.org/reference/random_.html) . You are using this overload: `random(low, high)` where you give low the value `RGB` and high the value `255`

RGB is just an integer that is equal to 1. It is simply used to be easier to read the code when you are changing the color mode because instead of writing `colorMode(1)` you can write `colorMode(RGB)` but even they seem different both are equivalent.

All that to say, that what you actually want to do is to create 3 random values and create a color based on them. For example:

```auto
void draw() {
  k = random(255);
  l = random(255);
  m = random(255);

  background(k, l, m);
}

```

As you can see I’m using k, l and m as name of my variables (no need for r, g, b) and I’m using the `random(high)` overload of the `random()` function so it will give me a value between 0 included and 255 excluded.

Also you can see I’m using the `background(v1, v2, v3)` overload to set the color based on my 3 random value.

---

<div class="post-metadata">

**Author:** ![Marga](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marga](https://discourse.processing.org/u/Marga)\
**Post date:** [April 28, 2021, 3:39pm UTC](https://discourse.processing.org/t/what-is-the-code-to-generate-a-random-and-automatic-color-in-the-background/29544/11 "2021-04-28T15:39:22Z")

</div>

Thank you very much for your help!!! I already got it! 😁
