ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# After effects expression that draws a random number that is limited and decreasing

FAQ   •   VIEW ALL
 After effects expression that draws a random number that is limited and decreasing on Sep 28, 2017 at 7:19:24 am

Hello,

I need to generate a random number, that is displayed as text on the screen. I need the number to be limited with a min and max value. The number needs to be limited to one decimal place, and the value needs to be decreasing. I need to generate 15 such numbers and they need to change every second.

I got as far as generating a random value within a limit that refreshes every second. I also got it limited to one decimal place.

Can someone help me with the decreasing of the value.

This is what i got now:

seedRandom(Math.floor(time), timeless = true);
random(125,115).toFixed(1)

 Re: After effects expression that draws a random number that is limited and decreasingon Sep 28, 2017 at 3:26:37 pm

Try this:

a = [];
seedRandom(index,true);
for (i = 0; i < 15; i++) a.push(random(115,125));
a.sort();
idx = Math.min(Math.floor(time),14);
a[14-idx].toFixed(1)

Dan

 Re: After effects expression that draws a random number that is limited and decreasingon Sep 29, 2017 at 11:43:26 am

Thank you Dan, it does just what i described. However when I set the min value to a two digit number like 90. It starts to behave different. After a few changes the number jumps to a higher value.

For instance

a = [];
seedRandom(index,true);
for (i = 0; i <15; i++) a.push(random(90,105));
a.sort();
idx = Math.min(Math.floor(time),14);
a[14-idx].toFixed(1)

Also with this expression the value starts below 100, no matter how high I set the max value.

Any idea why this is?

 Re: After effects expression that draws a random number that is limited and decreasingon Sep 29, 2017 at 9:30:08 pm

Sorry, I used the wrong sort(). Try this:

a = [];
seedRandom(index,true);
for (i = 0; i <15; i++) a.push(random(90,105));
a.sort(function(a, b){return a - b});
idx = Math.min(Math.floor(time),14);
a[14-idx].toFixed(1)

Dan