ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Storing Random Numbers to Variables to Calculate Percent of Change

FAQ   •   VIEW ALL Storing Random Numbers to Variables to Calculate Percent of Change on Oct 7, 2019 at 3:01:44 pm

I have a random number generator with a range from 800,000 to 900,000 linked to a text layer and it changes every second. I want to know is there a way of storing and holding previous values to a variable? Example: Lets say at second 1 the value of the text layer is 800,000 and on second 2 the value changes to 900,000. I want to store the first seconds value to a variable so I can calculate the percent of change. Re: Storing Random Numbers to Variables to Calculate Percent of Changeon Oct 7, 2019 at 4:03:40 pm

Expressions have no memory, so variables don't survive from one frame to the next. As a workaround, by manipulating the random number seed, you can recalculate the previous random number and use that to get the difference between the current and previous values. Something like this:

period = 1; // duration of each random number
seed = Math.floor(time/period);
seedRandom(seed,true);
nCur = random (800000,900000);
seedRandom(seed-1,true);
nPrev = random (800000,900000);
diff = nCur - nPrev

Dan