# Random Color Gen . Store , Use

**URL:** <https://discourse.processing.org/t/random-color-gen-store-use/32493>\
**Category:** Beginners\
**Created:** [September 28, 2021, 9:25pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493 "2021-09-28T21:25:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADG](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@ADG](https://discourse.processing.org/u/ADG)\
**Post date:** [September 28, 2021, 9:25pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493/1 "2021-09-28T21:25:20Z")

</div>

Hi, Very new to programming, and its concepts so I’m trying to create a random color generator that I can call anytime to provide my r,g,b values.

This is what I wrote:

```auto
let x;
let y;
let col = {
  r: [],
  g: [],
  b: [],
};

function setup() {
  createCanvas(600, 600);

  background(255);

}

function draw() {

  randomCol();
  fill(col.r, col.g, col.b);
  ellipse(mouseX, mouseY, 15, 15);

  randomCol();
  fill(col.r, col.g, col.b);
  ellipse(mouseX + 50, mouseY + 50, 15, 15);

}

function randomCol() {

  while (col.r < 1 && col.g < 1 && col.b < 1) {
    col.r = random(0, 255);
    col.g = random(0, 255);
    col.b = random(0, 255);

  }

}

```

My problem is that when I call the function I get the same color for every element. I tried using push and pop but that didn’t help. Can anyone shed light on why this doesn’t work?

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [September 28, 2021, 9:42pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493/2 "2021-09-28T21:42:49Z")

</div>

col should contain scalar values instead of arrays.

```auto
let x;
let y;
let col = {
  r: 0,
  g: 0,
  b: 0,
};

function setup() {
  createCanvas(600, 600);
  background(255);
}

function draw() {
  randomCol();
  fill(col.r, col.g, col.b);
  ellipse(mouseX, mouseY, 15, 15);

  randomCol();
  fill(col.r, col.g, col.b);
  ellipse(mouseX + 50, mouseY + 50, 15, 15);
}

function randomCol() {
    col.r = random(0, 255);
    col.g = random(0, 255);
    col.b = random(0, 255);
}

```

---

<div class="post-metadata">

**Author:** ![ADG](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@ADG](https://discourse.processing.org/u/ADG)\
**Post date:** [September 28, 2021, 11:39pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493/3 "2021-09-28T23:39:34Z")

</div>

Ohh Okay I see. Thanks.

So now the colours are all continuously changing. If I wanted one different colour for each would I not require an array?

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [September 28, 2021, 11:49pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493/4 "2021-09-28T23:49:33Z")

</div>

you can create an array or just separate variables if there is only a few.

_As separate variables_

```auto
let x;
let y;
let colorA;
let colorB;

function setup() {
  createCanvas(600, 600);
  background(255);
  
  colorA = getRandomColor();
  colorB = getRandomColor();
}

function draw() {
  fill(colorA.r, colorA.g, colorA.b);
  ellipse(mouseX, mouseY, 15, 15);

  fill(colorB.r, colorB.g, colorB.b);
  ellipse(mouseX + 50, mouseY + 50, 15, 15);
}

function getRandomColor() {
  //you probably need to floor these values
  return { r: random(255), g: random(255), b: random(255) };
}

```

_or as an array of colors_

```auto
let x;
let y;

let colors;

function setup() {
  createCanvas(600, 600);
  background(255);
  noStroke();

  var amtOfColors = 10;
  colors = [];
  for(var i = 0; i < amtOfColors; i++) {
    colors.push(getRandomColor());
  }
}

function draw() {
  var rectSize = width / colors.length;
  for(var i = 0; i < colors.length; i++) {
    let x = i * rectSize
    let y = height / 2 - rectSize / 2;
    let col = colors[i];
    fill(col.r, col.g, col.b);
    rect(x, y, rectSize, rectSize);
  }
}

function getRandomColor() {
  //you probably need to floor these values
  return { r: random(255), g: random(255), b: random(255) };
}

```

---

<div class="post-metadata">

**Author:** ![ADG](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@ADG](https://discourse.processing.org/u/ADG)\
**Post date:** [September 29, 2021, 3:46pm UTC](https://discourse.processing.org/t/random-color-gen-store-use/32493/5 "2021-09-29T15:46:03Z")

</div>

ohhh okay, awesome. I didn’t realize I could set a variable as a function!

Thank you!
