Creative Communities of the World Forums

The peer to peer support community for media production professionals.

  • Posted by Matt Bacon on June 18, 2018 at 7:32 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;}

    Dan Ebberts replied 8 years, 1 month ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    June 18, 2018 at 8:51 pm

    Try changing this:

    c=random(1,3);

    to this:

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

    Dan

  • Matt Bacon

    June 18, 2018 at 9:04 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

  • Dan Ebberts

    June 18, 2018 at 9:11 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

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy