# How to set random colors

please format code with </> button * homework policy * asking questions

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

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

Hi @Ipboro

Welcome to the forum!

I would recommend you to look the random method

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

``````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

1 Like

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

Could I also make that the colour changes slower?

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

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

``````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.

1 Like

Thanks a lot, that really helped me!