ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# multiple random numbers but never the same

FAQ   •   VIEW ALL
 multiple random numbers but never the same by Kabo Lamon Oct 24, 2017 at 11:08:37 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

 Re: multiple random numbers but never the sameon Oct 24, 2017 at 2:15:21 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-java...

Andrei
My Envato portfolio.

 Re: multiple random numbers but never the sameon Oct 24, 2017 at 3:43:34 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.