ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Random expression: force new number

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Michael Müller
Random expression: force new number
on Dec 28, 2019 at 5:48:18 pm

I would like to create an expression that gives me a new value every frame, meaning it shouldn't show the same value twice in a row. This turns out surprisingly difficult when you want to have a random number between 1 and 3 like me.
For example: I currently get an output per frame like this: 1,3,3,2,1,1,3,2
But I'd like to have something like this: 1,3,2,3,1,2,3,2,1

I have tried various comparison loops, if and while statements and but just can't figure out the root of the issue.

In case you're interested in my current code:

holdTime = framesToTime(1)
seed = Math.floor(time/holdTime);
seedRandom(seed, true);
x = Math.floor(random(1,4));
val = effect("Slider A")("Slider"); // Slider control which holds the number
valNow = val.valueAtTime(time); // Number at current time
valBefore = val.valueAtTime(time-(1/24)); Number of the frame before
//I assume there should follow some sort of comparison now
x;

Thanks a bunch

Michael


Return to posts index

Dan Ebberts
Re: Random expression: force new number
on Dec 28, 2019 at 8:04:27 pm

Since expressions have know way of accessing values they calculated on previous frames, you have to recreate the whole sequence, from frame 0 to the current frame, on each frame. Something like this should work, but you need to be aware that the expression has to do more calculation on each successive frame, so it may grind to a halt if your comp is too long:


seedRandom(index,true);
curVal = Math.floor(random(3))
f = timeToFrames(time);
for (i = 0; i <= f; i++){
curVal += Math.floor(random(1,3));
curVal %= 3;
}
curVal+1



Dan



Return to posts index

Michael Müller
Re: Random expression: force new number
on Dec 30, 2019 at 4:37:32 pm

Worked like a charm, thanks a bunch!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]