# Displaying randomised coloured squares

**URL:** https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824
**Category:** Coding Questions
**Tags:** homework
**Created:** [March 25, 2021, 8:45am UTC](https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824 "2021-03-25T08:45:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hwahwa](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@hwahwa](https://discourse.processing.org/u/hwahwa)
#### Post date: [March 25, 2021, 8:45am UTC](https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824/1 "2021-03-25T08:45:10Z")

</div>

Hello, I’m trying to display randomised coloured squares but the issue is, when I use my code outside of a void draw() function, it works fine, but when I put it in a void draw() function, the colours of the squares continues to change infinitely. I’m an absolute beginner and this is a component for a project I’m doing. Any help would be appreciated 🙂

Here is my code:

void setup(){  
size(500, 500);  
}

void draw(){  
background(100);  
rectMode(CORNER);  
fill(0, 255, 0);  
noStroke();

for (int y = 0; y \< 500; y = y + 25) {  
for (int x = 0; x \< 200; x = x + 25){  
float r = random(62, 225);  
fill(0, random(62, 225), 0);  
rect(y, x, 25, 25);  
}  
}

---

<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: [March 25, 2021, 8:51am UTC](https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824/2 "2021-03-25T08:51:58Z")

</div>

in theory you need to store the colors in an array and fill this once in setup() and use it in draw()

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 25, 2021, 8:57am UTC](https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824/3 "2021-03-25T08:57:29Z")

</div>

Hi,

Welcome to the forum! 😉

Remember that you can format your code by using the `</>` button when editing your message on the forum or use backticks : ``` your code ``` → `your code`

> [@hwahwa](#):
>
> the colours of the squares continues to change infinitely

By [definition](https://en.wikipedia.org/wiki/Randomness), randomness has the property so that :

> A random sequence of events, [symbols](https://en.wikipedia.org/wiki/Symbol) or steps often has no [order](https://en.wiktionary.org/wiki/order) and does not follow an intelligible pattern or combination.

But because the `draw()` function executes multiple times per second (roughly 60), then the `random()` function should return a different (unpredictable to be exact because it can be the same) value each time! 🙂

If you want to save the random colors once, you need to store them into variables, as described by @chrisir

---

<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: [June 15, 2021, 1:15pm UTC](https://discourse.processing.org/t/displaying-randomised-coloured-squares/28824/6 "2021-06-15T13:15:09Z")

</div>

> [@hwahwa](#):
>
> in a void draw() function, the colours of the squares continues to change infinitely.

Another way than I have described is to

- get rid of background() [Edited: I was wrong here. That won’t help you] or
- use noLoop()  
but since it is part of a bigger project I assume this won’t help you. (for different reasons)

```auto

// just use noLoop() here, program stops, colors stay the same
// (but won't help you, when you want to do other things with your Sketch)

void setup() {
  size(500, 500);
}

void draw() {
  background(100);
  rectMode(CORNER);
  fill(0, 255, 0);
  noStroke();

  for (int y = 0; y < 500; y = y + 25) {
    for (int x = 0; x < 200; x = x + 25) {
      float r = random(62, 225);
      fill(0, random(62, 225), 0);
      rect(y, x, 25, 25);
    }
  }

  noLoop();
}

```
