Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions multiple random numbers but never the same

  • multiple random numbers but never the same

    Posted by Kabo Lam on October 24, 2017 at 11:08 am

    Hi,

    i’m trying to make an animation whereas there are multiple numbers (00-99) and needed it be random .
    I’m aware of the method Math.random, but i need it to never show the same number twice at the same moment.
    For example, if the first layer number is 15, none of the other 25 layers should have an random number 15. It should be able to stop after 5 seconds.
    any ideas?

    Always learning

    Kabo Lam replied 8 years, 6 months ago 2 Members · 3 Replies
  • 3 Replies
  • Andrei Popa

    October 24, 2017 at 2:15 pm

    The way i would do it is this. First make a layer named “Number Control”. In the text source expression field write this
    var a = new Array;
    for (i = 0; i<100;i++) a[i]=i
    function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
    }

    return array;
    }
    shuffle(a);
    "["+a.toString()+"]"

    Then use this expression in each of your layer

    var x= eval(comp("Comp 1").layer("prim").text.sourceText.toString());
    if (time<5) x[index] else value

    credits for the shuffle function https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

    Andrei
    My Envato portfolio.

  • Andrei Popa

    October 24, 2017 at 3:43 pm

    I forgot to explain myself. What we basically do is take that 0-99 array and shuffle its elements. That way we do not get any double. And then we take a chunk out of it, correspondent with the indexes of your layer. This won’t work if any of your layer has the index bigger than 100.

    Andrei
    My Envato portfolio.

  • Kabo Lam

    October 27, 2017 at 8:39 am

    thanks you so much, it works wonderfully

    Always learning

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