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.
I am going to go inside the UKRAMEDIA.COM text layer, select the Source Text and hit S twice to solo it
Then I will ALT+CLICK (OPT+CLICK on a Mac) on the stopwatch icon to activate the expression
Inside the text box, we can either type our expression or we can go to Expression Helper -> Random Numbers -> random()
If we let go, you can see that it will give us random numbers.
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.
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).
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!
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]
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)
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(seed, timeless = false)
random(100, 400)
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)
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.
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.
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)
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.
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.
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.
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.
Then press R to see the Rotation property.
ALT+CLICK (CMD+CLICK on a Mac) on the Rotation stopwatch icon to activate the expression.
Paste the code we copied earlier inside the text box.
Math.floor(time/.2);
seedRandom(seed, timeless = false)
random(100, 400)
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.
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.