ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Random Color

FAQ   •   VIEW ALL
 Random Color on Jun 18, 2018 at 7:32:46 pm

Hello,

I would like a shape layer to randomly alternate between 4 specific colors. My if/else statement appears to be working (when I manually assign a value between 1-3 to the variable c, the color of my shape changes).

When I use
"seed = Math.floor(time);
seedRandom(seed,true);
c=random(1,3);"
c appears to take a value beyond 1-3 and hold that value across time.

I suspect I'm making some simple error, but my syntax looks correct! Any help is greatly appreciated.

-Matt

```seed = Math.floor(time); seedRandom(seed,true); //c=1; c=random(1,3); if (c==1) {[3,130,171,255]/255;} else if (c==2) {[128,207,217,255]/255;} else if (c==3) {[2,146,113,255]/255;} else {[255,102,0,255]/255;}```

 Re: Random Coloron Jun 18, 2018 at 8:51:12 pm

Try changing this:

c=random(1,3);

to this:

c = Math.floor(random(1,5));

Dan

 Re: Random Coloron Jun 18, 2018 at 9:04:22 pm

Thanks Dan!

That worked immediately. Do you mind explaining that to me? I'm clearly an expressions n00b and I really don't follow what just happened.

Is Math.floor ensuring that the random number is a whole number? I presumed "random(1,3)" would give me one of three integers: [1, 2, 3]

Regards,

Matt

 Re: Random Coloron Jun 18, 2018 at 9:11:29 pm

>Is Math.floor ensuring that the random number is a whole number?

Yes, it rounds down.

random(1,3) will give you a non-integer number greater than 1 and less than 3.

Math.floor(random(1,5)) will give you an integer 1, 2, 3, or 4.

Dan