# How to make a random RGB color value for variable?

**URL:** https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123
**Category:** Coding Questions
**Created:** [October 4, 2018, 11:30am UTC](https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123 "2018-10-04T11:30:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ProcessingDevin](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@ProcessingDevin](https://discourse.processing.org/u/ProcessingDevin)
#### Post date: [October 4, 2018, 11:30am UTC](https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123/1 "2018-10-04T11:30:49Z")

</div>

I have this code and i only get random greyscale colors.  
All the answers i’ve found on the internet don’t work.  
How do i do this?

```auto
color colorBall = color(0, 0, 0);
float xspeed = 5;
float yspeed = 4;
float circleX = 0;
float circleY = 0;

void setup() {
  size(640, 360);
}

void draw() {

  background(0);

  // CIRCLE

  displayBall();
  movingBall();
  bouncingBall();
}

void displayBall() {

  fill(colorBall);
  stroke(255);
  ellipse(circleX, circleY, 32, 32);
}

void movingBall() {
  circleX = circleX + xspeed;
  circleY = circleY + yspeed;
}

void bouncingBall() {
  if (circleY > height || circleY < 0) {
    yspeed = yspeed * -1;
    colorBall = (int)random(255);
  }
  if (circleX > width || circleX < 0) {
    xspeed = xspeed * -1;
    colorBall = (int)random(255);
  }
}

```

---

<div class="post-metadata">

### Author: ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)
#### Post date: [October 4, 2018, 12:01pm UTC](https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123/2 "2018-10-04T12:01:15Z")

</div>

Hey buddy, don’t forget to indent your code next time. Use the \</\> feature in the text box so that we can read your code better.  
The issue here is that you’re assigning a random value just to the greyscale channel. This will always happen when you assign one int number to a color variable. Try assigning random values to each channel of the color variable.

```auto
color myRandomColor = color(random(255), random(255), random(255));

```

---

<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: [October 4, 2018, 4:48pm UTC](https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123/3 "2018-10-04T16:48:39Z")

</div>

> [@ProcessingDevin](#):
>
> colorBall = (int)random(255);

`colorBall = (color) random(#000000);`

---

<div class="post-metadata">

### Author: ![ProcessingDevin](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@ProcessingDevin](https://discourse.processing.org/u/ProcessingDevin)
#### Post date: [October 4, 2018, 5:24pm UTC](https://discourse.processing.org/t/how-to-make-a-random-rgb-color-value-for-variable/4123/4 "2018-10-04T17:24:03Z")

</div>

The indenting doesn’t work when i edit my post now…  
Can you maybe tell me how to do it?  
Also the line of code gives this error message:

![44](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7ea4abe6f249c383e196cec2a945d2bcf2bd4a00.png)

“The value of the local variable “colorBall” is not used”

`colorBall = (color) random(#000000);` works

Thanks you both!
