ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Grid of Random Images / No Duplicates

FAQ   •   VIEW ALL
 Grid of Random Images / No Duplicates on Jan 8, 2019 at 3:08:08 pm

I have a grid of 9 images ( the grid is made of a precomposed image sequence duplicated 9 times ) - I would like to have a random image displayed in any given tile of the grid every few seconds, without ever having one tile show the same image as another. Feels tricky given that each is pulling from the same image sequence. As a bonus, I'd like to be able to add or remove images from the sequence as needed.

Is there any magic math that can accomplish this?

 Re: Grid of Random Images / No Duplicateson Jan 8, 2019 at 5:27:56 pm

Here's one way. I'm assuming your grid consists of layers 1 through 9. Add an invisible text layer as layer 10 and name it "control", and add this sourceText expression:

``` dur = 3; seg = Math.floor((time - inPoint)/dur); seedRandom(seg,true); a = [1,2,3,4,5,6,7,8,9]; for (i = 0; i < a.length; i++){ idx = i + Math.floor(random(a.length - i)); temp = a[i]; a[i] = a[idx]; a[idx] = temp; } a.join() ```

txt = thisComp.layer("control").text.sourceText;
splt = txt.split(",");
f = parseInt(splt[index-1],10);
framesToTime(f)

That should get you started.

Dan

 Re: Grid of Random Images / No Duplicateson Jan 9, 2019 at 5:00:35 pm

Works like a charm! Small glitch that I can easily override - every so often when all the tiles change, one tile will show the same image. The rest are random and no two tiles are ever showing the same image. I guess this is the nature of randomness.
Regardless, this was a tremendous help and I thank you your time, Dan!

 Re: Grid of Random Images / No Duplicateson Jan 9, 2019 at 6:51:56 pm

I do see one error in the text expression. The array should be initialized with 0-8 instead of 1-9:

``` dur = 3; seg = Math.floor((time - inPoint)/dur); seedRandom(seg,true); a = [0,1,2,3,4,5,6,7,8]; for (i = 0; i < a.length; i++){ idx = i + Math.floor(random(a.length - i)); temp = a[i]; a[i] = a[idx]; a[idx] = temp; } a.join() ```

That doesn't solve the issue though where the random frame assigned to a particular grid position may be the same one that was assigned there for the previous segment.

Dan