• | **The limits of expressions? Random and pseudo-random commands**
on Apr 30, 2016 at 8:50:19 pm |

I am interested to know if expressions are limited to the command list which appears in the drop down menu on a layer or if other commands can be added via scripts?

Specifically, I would like to build in the option of different forms of 'random' expressions - including pseudo-random sequences such as Halton. Alternatively can scripts be used to add extra functionality to the expressions library - or can scripts talk directly to expressions?

Many thanks

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 6:42:44 am |

Expressions are based on Javascript. You can write your own functions, like pseudo-random generators.

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 7:59:35 am |

That's great to know, can you direct me to any resources which would help explain how to develop functions?

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 11:58:24 am Last Edited By Kalle Kannisto on May 1, 2016 at 11:58:41 am |

**[Paul Rijkaard]** *"Can you direct me to any resources which would help explain how to develop functions?"*
http://www.w3schools.com/js/js_functions.asp

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 8:26:11 am |

I would like to produce several PRNG's to be used as utilities in after effects.

Would it be possible to translate the pseudo code below to work in expressions?

` FUNCTION (index, base)`

BEGIN

result = 0;

f = 1;

i = index;

WHILE (i > 0)

BEGIN

f = f / base;

result = result + f * (i % base);

i = FLOOR(i / base);

END

RETURN result;

END

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 12:19:08 pm |

Expressions include a random number generator **random()**

To translate your pseudocode into and expression, it would go like this:

`function my_function(ix, base) {`

// note I used "ix" as "index" is an expression reserved variable for the index number of the current layer

result = 0;

f = 1;

i = ix;

while (i>;0){

f = f/base;

result = result + f * (i % base);

i = Math.floor(i/base);

}

return result;

}

Two things of note:

1) By itself this does nothing, you want to call it *in the same expression*. For instance, add this to the end:

ix = 1234;

base = 4321;

my_function(ix, base)

2) This will only affect the hosting property. If you want to access the result from multiple locations, you can make it the value of a slider and pick whip it from the property you want to affect. *The value will be same*, since the value is calculated just once for that frame, and you're really calling the post-expression value of the slider. You can't call the function itself from another property. If you want the function to be called separately in several properties, you'll have to include it an expression *in each of them*.

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 12:27:08 pm Last Edited By Kalle Kannisto on May 1, 2016 at 12:28:17 pm |

As you can see there was only a couple of small differences between your pseudocode and the expression javascript, so you pretty much already know this stuff.

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 1, 2016 at 3:10:11 pm |

Many thanks Kalle this is very helpful.

To explain: I want to replace the standard random number generator with several PRNGs (pseudo-random number generators) including the one listed above (a Halton sequence)

My project involves using the cell pattern effect to produce animated noise fields consisting of single points.

The different PRNGs should produce different distribution of the points within the noise field. Ideally I would use the expression to replace the random seed in the effect.

do you think this sounds like it will work?

• | **Re: The limits of expressions? Random and pseudo-random commands** on May 2, 2016 at 5:32:59 am |

So each of your points is on a layer or it's own? I don't see why not. And it seems you would want to use the layer index.

Taking a quick look at the Halton Sequence in wikipedia I see that you get a more even distribution that way -- if you're using it for position. If so, you'll probably just get the initial position values for the dots with it, and will need to use wiggle to actually animate them. You'd put the code on one of the dots -- although you'll have to modify it so that you get two dimensions instead of one. To add a wiggle, I believe you would first get the wiggled value, subtract the plain value and then add the Halton Sequence random values, all in the same expression, that gets duplicated for every layer.

If you just want it to generate a random seed, it seems a bit complex method to go about it, as you could use layer index for that purpose directly. But it should work for that, too.

Either way, you'd start with one dot, get the expression working there and then duplicate the layer it as many times as you want.