ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Random number and hold, then smooth transition to another number

FAQ   •   VIEW ALL
 Random number and hold, then smooth transition to another number on Feb 24, 2017 at 2:08:39 pmLast Edited By Pablo Levin on Feb 24, 2017 at 2:20:32 pm

I'm trying to choose between a range of numbers randomly. Once I choose a number I want to stay in that number for Xtime. But when is time to choose another number I want to smoothly increase or decrease to that number during Ytime. Once I reach the number hold it for Xtime again, etc.

Example:
4 --------------------4 --6--8--10--12--14----------------------14
hold Xtime transition Ytime hold Xtime

I don't know if something like this is posible.

```Xtime=effect("Xtime")(1); Ytime=effect("Ytime")(1); if(Xtime<=0){ Xtime=0.1; } seedRandom(Math.floor(time/Xtime), timeless = true); parseInt(random(0,56));```

 Re: Random number and hold, then smooth transition to another numberon Feb 24, 2017 at 2:28:50 pm

Try this:

Xtime=effect("Xtime")(1);
Ytime=effect("Ytime")(1);
period = Xtime + Ytime;

Xtime = Math.max(Xtime,.1);
seg = Math.floor(time/period);
t = time%period;
seedRandom(seg,true);
v1 = random(0,56);
seedRandom(seg+1,true);
v2 = random(0,56);
linear(t,Xtime,period,v1,v2)

Dan

 Re: Random number and hold, then smooth transition to another numberon Feb 24, 2017 at 5:35:58 pm

Awesome Dan thanks a lot!!!

 Re: Random number and hold, then smooth transition to another numberon Feb 24, 2017 at 6:02:56 pm

Is it posible to detect when I'm in a hold moment or in a transition moment?

For example if it's a hold moment state=1, and if it's a transition moment state=0.

Thanks again!!

 Re: Random number and hold, then smooth transition to another numberon Feb 24, 2017 at 6:36:33 pm

I guess that would be like this:

Xtime=effect("Xtime")(1);
Ytime=effect("Ytime")(1);
Xtime = Math.max(Xtime,.1);
period = Xtime + Ytime;
t = time%period;
t < Xtime ? 1 : 0

This version has a correction that you should make to the previous version (swap the 3rd and 4th lines).

Dan

 Re: Random number and hold, then smooth transition to another numberon Feb 24, 2017 at 9:46:44 pm

Thanks Dan!!!

 Re: Random number and hold, then smooth transition to another numberon Mar 12, 2017 at 10:25:30 pm

Hello,

So I generate random flashes of light (color balance) with this expression based on audio levels:

How can hold that value longer and then ease out smoothly? Tried to use method above just by copy/paste, but it does not work, it says expression error. I'm kind of new in this, and I'm sure there are better methods to do these random flashes, so any help will be appreciated.

Thanks!

```L=thisComp.layer("Sound Keys").effect("Sound Keys")("Output 1") // Sound keys output rnd=(random(1,100)) if(rnd>95) random(50,100) else 0; if (L&lt;45) 0 // If output is > 45```