How To Generate Random Numbers in After Effects

In this post, you will learn how to generate random numbers in After Effects.

Alright, so we are in After Effects. I will warn you, this tip is not the most exciting tip, however, I do find it very useful. Let me show you why.

After Effects generating random numbers
I am going to go inside the UKRAMEDIA.COM text layer, select the Source Text and hit S twice to solo it

Select Source Text in After Effects

Solo in After Effects keyboard shortcut

Then I will ALT+CLICK (OPT+CLICK on a Mac) on the stopwatch icon to activate the expression

Activate the expression in After Effects

Inside the text box, we can either type our expression or we can go to Expression Helper -> Random Numbers -> random()

Select random() number in After Effects with expression helper

If we let go, you can see that it will give us random numbers.

Random numbers in After Effects
It is giving me a random number between 0 and 1 because I do not have anything inside of random() parentheses.

Now if I type something like 100, it will give me a random value between 0 and 100, so if I scroll through in the timeline with the time indicator, you can tell that it is doing exactly that.

1 through 100 random numbers in After Effects
Let’s keep going with this. So if I wanted a rand0m value between 100 and 500. Meaning, I do not want the value to go below 100 or higher than 500. So to do that I would simply write random(100, 500).

Value between 100 and 500 in random numbers in After Effects

As you can see, it is working quite well. Now the cool thing about this is that you can use it on any value and you can also use it on arrays. Let me show you what I am talking about here.

So I can type something like this:

random([10,10],[50,50])

If I let go, you can see that it is now giving me two numbers, which is awesome!

Random numbers square brackets in After Effects

You can call it out any time. For example, let’s say I want an array index 0. To do that, I will add [0] to the end of random() and it is going to go to the first one.

random([10,10],[50,50])[0]

Index 0 in after effects

You can also do more than one area. You can do three, four and so on.

Let me show you something else here, so I am going to do a random number between 100 and 400. Here are our numbers:

random(100,400)

Random number between 100 and 400 in After Effects

What if I wanted to generate a random seed for each frame? So right now I have this value 117.025276044622 in my composition, but how do I change the seed of it? Let me show you!

To change the seed, we are going to add something extra to our expression. We are going to go back to the text editor, hit enter to add space and then inside of our text editor I can either type the expression or I can click on the Expression Helper, go to Random Numbers and then select seedRandom(seed, timeless = false).

seedRandom After Effects

seedRandom(seed, timeless = false)

random(100, 400)

seedRandom in Adobe After Effects

We can change the seed inside of seedRandom() to any value. We can say 5. Now watch what happens to
our value here.

seedRandom(5, timeless = false)

random(100, 400)

seedRandom in After Effects Ukramedia

As you can see, it generated a different number. So I can keep changing the seed to a different number like 6 and it will keep giving me a different value.

Different value seedRandom in After Effects

That is pretty cool. Now the second portion of this is called timeless. Right now it is set to false and because of that we have a different value for every single frame, however, if I change timeless to true, things will change.

We will no longer get a different value for every single frame. We will instead be stuck with one value for each frame. It will be the same number regardless of where the time indicator is set to.

seedRandom timeless set to true in After Effects

timeless set to true in After Effects

In other words, it is only generating one value but you can still offset the seed. For example, you can change the seed 6 to 8 and it is going to give you a different seed, but it is not changing for every single frame. So that can definitely be very useful.

seedRandom(8, timeless = false)

random(100, 400)

Different seed in After Effects

Different seed in Adobe After Effects

Now the cool thing about this is that you can animate the seed value. So right here I am going to give it a variable seed and I am going to define my seed by saying seed, you are going to be time divided by .5.

seed = time/.5;

seedRandom(seed, timeless = false)

random(100, 400)

When you do time divided by 0.5, it still gives you a value for every single frame. That is
problematic.

time divided by in After Effects

If we round it up or let’s round it downwards instead. I am going to say Math.floor(time/5) and then watch
what happens now.

Math Floor rounding in After Effects

Rounding in After Effects

Now 0 holds on for a couple of frames and then it changes to 1 and that 1 stays on for a couple of frames and then changes again. In other words, getting rid of decimals gave us something entirely different which is exactly what I am after.

So I am going to select and copy Math.floor(time/5).  Next, I am going to go back to where we were before:

seed = time/.5;

seedRandom(seed, timeless = false)

random(100, 400)

So now we are going to paste Math.floor(time/5) inside of seed = time/.5

Math.floor(time/5);

seedRandom(seed, timeless = false)

random(100, 400)

Watch what happens now.

After Effects

Adobe After Effects

So every half a second or so the value is changing. That can be interesting. It is almost like a stop-motion random animation type deal. We can also speed it up.

We can change it to .2 instead of 5 to give us a value quicker.

Math.floor(time/.2);

seedRandom(seed, timeless = false)

random(100, 400)

Now, this can be interesting if you apply it to some property, so I am going to select all of my code and copy it.

Copy in After Effects

Then press R to see the Rotation property.

Rotation in After Effects

ALT+CLICK (CMD+CLICK on a Mac) on the Rotation stopwatch icon to activate the expression.

Activate Expression in After Effects

Paste the code we copied earlier inside the text box.

Math.floor(time/.2);

seedRandom(seed, timeless = false)

random(100, 400)

Paste in After Effects

Let’s do a few minor changes to the code. Let’s change 100 to 90 and then 400 to -90.

Math.floor(time/.2);

seedRandom(seed, timeless = false)

random(90, -90)

So it is going to give us a value between 90 and negative 90. It will give us almost like a stop-motion effect.

Stop motion in After Effects

stopmotion Effect in After Effects

Again, we can speed it up by changing the Math.floor(time/.2) to Math.floor(time/.1) or we can slow it down.

So yeah, this can definitely be very handy.

I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.

About the author:

Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!