Conditional Logic using Chains and Random Numbers


// Generates a random integer (whole number) contained within the
// two extremes (inclusive on lower extreme, exclusive on upper).
//
// Example usage:
//   var num = generateRandomNumber(0,5);
//        This will generate a random number, either 0, 1, 2, 3 or 4
//
//   var num = generateRandomNumber(5,8);
//        This will generate a random number either 5, 6 or 7
//
function generateRandomNumber(lowerExtreme, upperExtreme) {
   return Math.floor((Math.random() * (upperExtreme - lowerExtreme))  
+ lowerExtreme);
}

// End Function

// "DeleteMovie" is the name of the chain in my Clip.
var chain = $context.currentClip.getChild("DeleteMovie");

// Generate random number betwen 1-10
var num1 = "" + generateRandomNumber(1,11);
$context.result.postMessage($context.result.LEVEL_INFO, "RandomNum=",  
num1);

// If we get a 1 the Chain plays. If we get 2-10 the Chain is set to  
// Zero and will not play.
// This satisfies a 10% of the time I need to perform an action. 90%  
// of the time this chain will not play.
if (num1 != 1)
{
  chain.setRepeat(
  chain.REPEAT_TIMING_SERIAL,
  chain.REPEAT_TYPE_COUNT_CONSTANT,
  0, //THIS means do it ZERO times
  chain.REPEAT_DISTRIBUTION_CONSTANT,
  0);
}