# Perhaps I do not know fill/random works after all

**URL:** <https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424>\
**Category:** Coding Questions\
**Created:** [January 29, 2020, 7:11pm UTC](https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424 "2020-01-29T19:11:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangs](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@yangs](https://discourse.processing.org/u/yangs)\
**Post date:** [January 29, 2020, 7:11pm UTC](https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424/1 "2020-01-29T19:11:35Z")

</div>

Creating a very rough sketch of a generative rothko – three or two rectangles of random size within a range on a muted background – and I’ve come to the conclusion I don’t understand how `fill()` and/or `random()` works.

However, what happens is that all the rectangles are the same randomized color. Was I mistaken in thinking that writing `fill()` next to each `rect()` would have generated a different color? Can anyone point me in the direction of how to get each of these rectangles to be differently colored or maybe another resource on how fill actually works?

Thank you!

```auto
let rectX = 30;
let width = 430;

let r;
let g;
let b;

function setup() {

  r = random(255);
  g = random(255);
  b = random(255);

  createCanvas(500, 500);
  background(0);

  fill(r, g, b);
  rect(rectX, 20, width, random(80, 100));
  
  fill(r, g, b);
  rect(rectX, 200, width, random(25, 250));
  
  fill(r, g, b);
  rect(rectX, 400, width, random(55, 80));
  
}
```

---

<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:** [January 29, 2020, 7:28pm UTC](https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424/2 "2020-01-29T19:28:42Z")

</div>

> [@yangs](#):
>
> r = random(255);  
> g = random(255);  
> b = random(255);

You need to repeat this every time

---

<div class="post-metadata">

**Author:** ![yangs](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@yangs](https://discourse.processing.org/u/yangs)\
**Post date:** [January 30, 2020, 10:06pm UTC](https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424/3 "2020-01-30T22:06:57Z")

</div>

Thanks! Is there a reason for this in terms of how p5js reads this? I would have assumed that the variables would have been randomized since they were created globally.

Also is there an obvious way I’m missing to make my code dry-er? There is a lot of repetition as of now.

---

<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:** [January 30, 2020, 10:10pm UTC](https://discourse.processing.org/t/perhaps-i-do-not-know-fill-random-works-after-all/17424/4 "2020-01-30T22:10:46Z")

</div>

r and g and b only change the color when you tell them to. That’s what the 3 lines do. But they don’t change the color throughout but only when you tell them to.

Hence you need to repeat the lines every time you want them to have new values
