Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How do I use seedRandom with a Sine or Cos function to make a pendulum swing start position random?

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

    Posted by Ian Harper on November 14, 2018 at 7: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)

    Alex Printz replied 7 years, 6 months ago 3 Members · 7 Replies
  • 7 Replies
  • Kalleheikki Kannisto

    November 14, 2018 at 7:57 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

  • Ian Harper

    November 14, 2018 at 8:37 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?

  • Alex Printz

    November 14, 2018 at 9:49 pm

    change the line

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

    Alex Printz
    Mograph Designer

  • Alex Printz

    November 14, 2018 at 9:56 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

  • Kalleheikki Kannisto

    November 14, 2018 at 10:29 pm

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

    Kalleheikki Kannisto
    Senior Graphic Designer

  • Ian Harper

    November 15, 2018 at 3: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)

  • Alex Printz

    November 15, 2018 at 4:14 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

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy