ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?

FAQ   •   VIEW ALL
 How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random? on Nov 14, 2018 at 7:25:25 pm

I wrote a bit of code to make ornaments sway back and forth on a tree, but they all swing in unison. I was trying to put in a random seed to make the sine wave start at a different position but it just makes it randomly rotate to different positions. I have a temporary solution of giving each object a different offset, it looks good but I bet there is a better way.

```freq = .5; //oscillations per second amplitude = 20; decay = 0; //no decay amplitude*Math.sin(freq*time*2*Math.PI+"**I think this is where the offset should go**")/Math.exp(decay*time)```

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 14, 2018 at 7:57:15 pm

If every ornament is on its own layer, you can use the layer index to seed the random number.

```freq = .5; //oscillations per second amplitude = 20; decay = 0; //no decay seedRandom(index); // random seed based on layer index offset = random(2*Math.PI); // offset amount for this layer amplitude*Math.sin(freq*time*2*Math.PI+offset)/Math.exp(decay*time)```

Kalleheikki Kannisto
Senior Graphic Designer

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 14, 2018 at 8:37:13 pm

This is doing the same thing as my inserting of random() in the phase section. All the ornaments are shaking like they have Parkinson's. I think the problem is that each time the frequency resets, the random seed is spitting out a random number. I only need it to happen once at the beginning of the composition.

I also have a question about the seedRandom(index); I see it generates a random seed based on layer index but I don't see how it's effecting the amplitude formula. Is it generating a different instance for each layer? If so can we get rid of the "offset" variable?

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 14, 2018 at 9:56:07 pm

Also per your question, the amplitude is not being affected at all in that formula; if you want it to be, you're going to want to add a random() to it as well; probably related to the frequency in some way.

Alex Printz
Mograph Designer

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 14, 2018 at 10:29:36 pm

Sorry, forgot to add the timeless (true) attribute there, that'll fix it.

Kalleheikki Kannisto
Senior Graphic Designer

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 15, 2018 at 3:53:53 pm

Adding the true fixed it. But I'm still unsure how the seedRandom(index,true); give each layer a different starting point without being in the actual formula. Normally I've seen where you would have a variable like "seed" then you put seedRandom(seed,true); to give that seed a random value to be inserted in into other another formula like "segStart = seed*segDur;" but your code seems to be a function not associated with the sine function.

```freq = .5; //oscillations per second amplitude = 20; decay = 0; //no decay seedRandom(index,true); // random seed based on layer index but how does it give each layer a different starting point? offset = random(2*Math.PI); // offset amount for this layer amplitude*Math.sin(freq*time*2*Math.PI+offset)/Math.exp(decay*time)```

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 15, 2018 at 4:14:47 pm

seedRandom sets a seed, and all code after that will use that seed when you call a property that requires a seed to start (random, wiggle, etc.). If you need to use multiple random seeds, i've used something like this before:

```seedRandom(index,true); X = wiggle(2,2); seedRandom(random(0,index*2),true); Y = wiggle(2,2); [X[0],Y[0]]```

A lot of people like to use the index to set a seed randomly, so that they can duplicate layers and it's new random variables instantly. If I need to make sure that specific layers always use the same seeds no matter if I move them around (e,g, I have a character that needs to avoid a random swinging thing and that layer's index might change as I animate), I will convert the layer names to digits and then use that, but it requires more computations so I don't suggest it unless necessary.

Alex Printz
Mograph Designer

 Re: How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?on Nov 14, 2018 at 9:49:44 pm

change the line

`seedRandom(index);`
to
`seedRandom(index,true);`

Alex Printz
Mograph Designer