Random properties

WIP - last updated 21 April 2021

Abstract

One of the most useful things you can do with Expressions is randomizing layer properties. Some properties include transform properties (position, rotation, scale, or opacity), color, or even Time Remap. Learning to randomize properties is a good beginner exercise to raise one's comprehension of Javascript Expression. In this page, we will cover different ways to randomize properties.

Understanding Random functions

  • random()

  • random(minValue, maxValue)

  • seedRandom(seed, timeless=false)

Core skills: randomize values, arrays, color

What is a seed? A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator. To put simply, different seed number yield difference sequence of random number.

Position

Wiggle

wiggle(20,50);

// wiggle x axis 
x = wiggle(20,50)[0];
y = value[1];
pos = [x,y]

// wiggle y axis 
x = value[0[;
y = wiggle(20,50);
pos = [x,y]

// getting wiggle increment values only 
inc = wiggle(20,50)-50;

positioning

// randomize between a range 
seedRandom(1,true);
x = random(300,1920);
y = random(500,1080);
[x,y];
// using arrays and variable 
seed = 20;
min = [25,25,-1900]; 
max = [thisComp.width,thisComp.height,50]

seedRandom(seed,true);
random(min,max);

/* This is same as this
x= random(min[0],max[0]);
y= random(min[1],max[1]);
z= random(min[2],max[2]);
[x,y,z];
*/

Opacity

Flickering / Strobe

// VARIABLES
    minSeg = 1.5; //minimum interval (must be > 0) 
    maxSeg = 2.5; //maximum interval (must be > minSeg) 
    
    // flickering duration
    minFlicker = .5; //must be less than minSeg 
    maxFlicker = 1; // must be less than minSeg 
    flickerDur = random(minFlicker,maxFlicker);
    
    //initial conditions
    segEndTime = 0; 
    i = 1; 

// Continuous loop: create a fixed random segment value and add to segEndTime
while (time >= segEndTime){ 
    i += 1; 
    seedRandom(i,true); 
    segEndTime = segEndTime + random(minSeg,maxSeg); 
} 

// Switch back to use the current time as input to the random seed.
seedRandom(1,false); 

// As time > threshold, flicker
if (time > segEndTime - flickerDur){random(0,100) }else{ 100 }

// Source: http://www.motionscript.com/expressions-lab-ae65/swinging-light.html
// Also see: http://www.motionscript.com/mastering-expressions/random-3.html

Wiggle

// control = thisComp.layer("control"); // connect to null layer with sliders
freq = 1;
amp = 100;
octave = 1;
amp_mult = 3;

wiggle(freq, amp, octave, amp_mult, time)

Wiggle with flicker

// control = thisComp.layer("control"); // connect to null layer with sliders
freq = 1;
amp = 100;
octave = 1;
amp_mult = 3;

opacity = wiggle(freq, amp, octave, amp_mult, time)


// VARIABLES
minSeg = control.effect("minSeg")("Slider"); //minimum interval (must be > 0) 
maxSeg = control.effect("maxSeg")("Slider");; //maximum interval (must be > minSeg) 

// flickering duration
minFlicker = control.effect("minFlicker")("Slider");; //must be less than minSeg 
maxFlicker = control.effect("maxFlicker")("Slider");; // must be less than minSeg 
flickerDur = random(minFlicker, maxFlicker);

//initial conditions 
segStartTime = 0;
segEndTime = 0;
i = 1;

// Continuous loop: create a fixed random segment value and add to segEndTime
while (time >= segEndTime) {
    i += 1;
    seedRandom(i, true);
    segStartTime = segEndTime;
    segEndTime = segEndTime + random(minSeg, maxSeg);
}

// Switch back to use the current time as input to the random seed.
seedRandom(1, false);

// As time moves threshold, flicker
if (time > segEndTime - flickerDur) {
    random(0, 100)
} else {
    opacity
}

Using sine function

// Mass flickering
vel = 50;
seedRandom(0,true);
Math.sin(time*vel+random(index))*100;
// 'Wave flickering'
vel = 50;
seedRandom(0,true);
Math.sin(time*vel+index)*100;

// Combining 
bool = 1;
vel = 50;
seedRandom(0,true);
wave=Math.sin(time*vel+index)*100;
rand =Math.sin(time*vel+random(index))*100;
bool==0 ? wave : rand;

Random pop on

seed = 29;
threshold = linear(time,0,thisComp.duration,0,100); // or Connect to slider to animate switch on & off

seedRandom(seed,true);
randValue = random(0,90);

if(randValue < threshold){
    100;} else 0

Tutorial: Randomly Reveal Layers & Emojis with Expressions, NSC

Random fade on

var seed = 1;
var seedRandom(seed,true);

var startTime = 0; // specify when fade starts
var delay = random(0,1); // random delay
var fadeDur = framesToTime(20); // duration of fade

linear(time, startTime + delay, startTime + delay + fadeDur, 0, 100)

Random Color

RGB color

// Apply on a color property eg. Fill, Shape Fill
seedRandom(1,true);
random([0,0,0,1],[1,1,1,1])

From a color palette

Sequential cycling color from a palette

// user variables
var vel = 2 ;
var numCol = 5;

//main
var inc = Math.floor(time * vel) + index; // counter
var n = inc % numCol + 1; // keep the num between 1 & numCol value

// reference a null with colors controls only 
thisComp.layer("controls").effect(n)("Color"); 
  1. retrieve the absolute index of a layer,

  2. add a slider that increment by a value at a set interval

  3. do the mod calculation

Random cycling color from a palette

// user variables
var mySpeed = posterizeTime(3);
var colorNull = thisComp.layer("controls"); 

// calculate num of color controls
var numFx = colorNull("ADBE Effect Parade").numProperties;
var numCol = 0;
for (i = 1; i <= numFx; i++) {
    if (colorNull.effect(i).name.includes("Color")) {
        numCol += 1;
    }
}

// main
var n = Math.ceil(random(numCol));
thisComp.layer("controls").effect(n)("Color");

Get num of color controls

var colorNull = thisLayer("ADBE Effect Parade");
var numFx = colorNull.numProperties;
var numCol = 0;
for (i = 1; i <= numFx; i++) {
    if (thisLayer.effect(i).includes("Color")) {
        numCol += 1;
    }
}
numCol;

Time Remapping / Playback

Random frame playback

// Source Dan Ebbert

fr = 12; // frame rate;

numFrames = 8;

seedRandom(index,true);

seg = Math.floor(time*fr);

f = Math.floor(random(numFrames));

for (i = 0; i < seg; i++)

  f = (f + Math.floor(random(1,numFrames)))%numFrames;

framesToTime(f);

Random delayed/advanced layer playback

var seed = 1;
var seedRandom(seed,true);
var myDelay = random(1,10);
var time - myDelay;

Play one frame at a time (without 'float time')

var fps = 1;
x = framesToTime(Math.round(time*fps))
//There probably is a better way but I cannot figured it out right now.

Random still frame (Good for spriting)

//Apply this to a composition with a image sequence inside
seedRandom(0,true);
t= random(0,72);
framesToTime(t);
  • for spriting images,

  • refer to CoMotion 2021 title sequence background

Source text & strings

var nums = [1,2,3,4,5,6,7,8,9,10],//all numbers to be randomized
    ranNums = [],
    i = nums.length,
    j = 0;

while (i--) {
    j = Math.floor(Math.random() * (i+1));
    ranNums.push(nums[j]);
    nums.splice(j,1);
}
ranNums

Using text animators

Binary

From a specific set of characters or words

eg. Matrix katakana

Building a Randomizing rig or preset

Core skills: randomize time, Spriting, building controls, conditional randomizing with modulo operator (alternate colors)

Keep in After Effects scripting, the random function only returns a random number between 0 to 1. What this means is that you cannot do use random(20,30) to generate a random value between 20 and 30. You have to write a function for that, which you can reference here.

Randomizing with AE scripts

  1. Download Jeff Almasol's script from aescript or redefinery

  2. Install script via After Effects or putting directly into the Applications folder

  3. Modify values

  4. Select layers, and run script file

Last updated