Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random order of the numbers 0,1,2,3,4,5,6,7,8

  • Random order of the numbers 0,1,2,3,4,5,6,7,8

    Posted by Steven Fokkinga on September 25, 2016 at 9:40 am

    Dear all,

    I have a composition with 9 colored squares next to each other, and I want to randomly change the order of these squares every second. I’ve created nine expression sliders with integer values that I have related to the position of the squares (multiplied by the pixel width of the squares, so square 0 has X-position 0*1024, square 1 has position 1*1024, and so forth).
    The challenge is getting nine random numbers without duplicates (because then two or more squares would overlap). How could I get nine random numbers without duplicates? Basically, how could I get a random order of the number series 0,1,2,3,4,5,6,7,8 every second?

    Thanks in advance!

    Steven Fokkinga replied 9 years, 7 months ago 3 Members · 3 Replies
  • 3 Replies
  • Kalleheikki Kannisto

    September 25, 2016 at 1:44 pm

    In essence you need two loops, one that fills the array with a random number and another inside that which checks if that number has already been used and keeps adding one (or wrapping from 8 to 0) until it finds an unused number.

    Kalleheikki Kannisto
    Senior Graphic Designer

  • Dan Ebberts

    September 25, 2016 at 3:22 pm

    The trick is to get all the layers to cooperate on random numbers. To do that, you need to do the random calculation in a single place, and publish the results for all the participating layers to reference. One way is to create a text layer (named “control” in this example), move it to the bottom of the layer stack, and add this Source Text expression:


    period = 1;
    seed = Math.floor(time/period);
    seedRandom(seed,true);
    a = [];
    for (i = 0; i < 9; i++) a[i] = i;
    for (i = 0; i < a.length; i++){
    idx = Math.floor(random(a.length));
    temp = a[i];
    a[i] = a[idx];
    a[idx] = temp;
    }
    a.toString();

    (You can turn the text layer off once you get everything set up). For the next part, I’m assuming your participating layers are layers 1-9–if not, you’ll have to modify this position expression:


    str = thisComp.layer("control").text.sourceText.split(",");
    idx = parseInt(str[index-1],10)
    x = idx*1024;
    [x,value[1]]

    Dan

  • Steven Fokkinga

    September 26, 2016 at 9:57 am

    Thank you for your very elaborate suggestions! I managed to make it work!

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