# How to set random colors

**URL:** <https://discourse.processing.org/t/how-to-set-random-colors/25627>\
**Category:** Coding Questions\
**Created:** [November 23, 2020, 8:49am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627 "2020-11-23T08:49:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ipboro](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Ipboro](https://discourse.processing.org/u/Ipboro)\
**Post date:** [November 23, 2020, 8:49am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/1 "2020-11-23T08:49:56Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hello,  
So I was wondering how can I (or if i can) randomize the colors in the set of code.  
This is my code for now, it is basiclly parametric lines.

static final int NUM\_LINES = 30;

float t;

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

void draw() {  
background(10);  
stroke(225);  
strokeWeight(4);

translate(width/2, height/2);

for (int i = 0; i \< NUM\_LINES; i++) {  
line(x1(t + i), y1(t + i), x2(t + i), y2(t + i));  
}  
t += 0.4;  
}

float x1(float t) {  
return sin (-t / 10) \* 100 + sin (t / 6) \* 20 ;  
}

float y1(float t) {  
return cos (-t / 10) \* 100;  
}

float x2(float t) {  
return sin (-t / 10) \* 100 + sin (t) \* 2 ;  
}

float y2(float t) {  
return cos (-t / 20) \* 200 + cos(t / 12) \* 20;  
}

I have tried many different ways but none of them seem to work. I would appreciate any help with the code.  
Thanks

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 23, 2020, 9:05am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/2 "2020-11-23T09:05:13Z")

</div>

use `color col = color(random(255), random(255), color(255));`

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [November 23, 2020, 9:07am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/3 "2020-11-23T09:07:38Z")

</div>

Hi @Ipboro

Welcome to the forum!

I would recommend you to look the random method

> **[random() \\ Language (API) \\ Processing 3+](https://processing.org/reference/random_.html)**

Then, you can define the colors and assign it to the stroke

```auto
int R = (int)random(0,255);
int G = (int)random(0,255);
int B = (int)random(0,255);

stroke(color(R,G,B));

```

Hope it helps!  
Best regards

---

<div class="post-metadata">

**Author:** ![Ipboro](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Ipboro](https://discourse.processing.org/u/Ipboro)\
**Post date:** [November 23, 2020, 9:17am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/4 "2020-11-23T09:17:19Z")

</div>

Thanks a lot! It helped 🙂  
Should have looked there first, but anyway I would like to thank you again for the help!

---

<div class="post-metadata">

**Author:** ![Ipboro](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Ipboro](https://discourse.processing.org/u/Ipboro)\
**Post date:** [November 23, 2020, 9:23am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/5 "2020-11-23T09:23:10Z")

</div>

Could I also make that the colour changes slower?

---

<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:** [November 23, 2020, 9:28am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/6 "2020-11-23T09:28:05Z")

</div>

1 statement **random()** `color` value: `color c = (color) random(#000000);` 🎨

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [November 23, 2020, 9:43am UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/7 "2020-11-23T09:43:10Z")

</div>

You can use a timer to change it only from N to N time intervals

```auto
static final int NUM_LINES = 30;

float t;
int R, G, B;
long previousTime = 0, currentTime = 0;

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

void draw() {
  background(10);
  
  
  currentTime = millis();
  if(currentTime - previousTime > 1000){ //If the time interval between update is higher than 1s (1000ms) then get the new colors
   R = (int)random(0,255);
   G = (int)random(0,255);
   B = (int)random(0,255);
   previousTime = currentTime;
  }
  
  stroke(color(R, G, B));
  strokeWeight(4);

  translate(width/2, height/2);

  for (int i = 0; i < NUM_LINES; i++) {
    line(x1(t + i), y1(t + i), x2(t + i), y2(t + i));
  }
  t += 0.4;
}

float x1(float t) {
  return sin (-t / 10) * 100 + sin (t / 6) * 20 ;
}

float y1(float t) {
  return cos (-t / 10) * 100;
}

float x2(float t) {
  return sin (-t / 10) * 100 + sin (t) * 2 ;
}

float y2(float t) {
  return cos (-t / 20) * 200 + cos(t / 12) * 20;
}

```

If you want them to change smoothly I would suggest you look at the lerpColor method. There you can decide the initial and final color and get all the ones in between by setting the amt parameter.

> **[lerpColor() \\ Language (API) \\ Processing 3+](https://processing.org/reference/lerpColor_.html)**

---

<div class="post-metadata">

**Author:** ![Ipboro](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Ipboro](https://discourse.processing.org/u/Ipboro)\
**Post date:** [November 23, 2020, 1:46pm UTC](https://discourse.processing.org/t/how-to-set-random-colors/25627/8 "2020-11-23T13:46:25Z")

</div>

Thanks a lot, that really helped me!
