| **Explain this function?**
on May 4, 2012 at 10:36:58 pm |

Hi guys,

This is my first post! I'm studying a book by Marcus Geduld named After Effects Expressions and ran into a function that confuses me. I'm just wondering if someone is able to help me break it down to the basics. The purpose is to randomly pick a different value for rotation on every frame.

I'd appreciate any help, cheers.

`function pickRandomNumber(arrayOfNumbers) // Define the function and the input.`

{

var randomNumber = random(arrayOfNumbers.length); // Randomly generate numbers, but within the range (length) of arrayOfNumbers?

var myIndex = Math.floor(randomNumber); // Take the numbers generated from randomNumber and round them down?

return arrayOfNumbers[myIndex]; // I don't understand what is happening here. Are we determining how this part of the function works or will accept myArray?

myArray = [10,20,30,40,50,60]; // Got it, this is an array of numbers.

pickRandomNumber(myArray); // Call the pickRandomNumber function and use myArray as the input?

| **Re: Explain this function?** on May 4, 2012 at 11:31:07 pm |

The first line of the function generates a random number between 0 and 5.99999... (the upper limit is slightly less than the length of the array).

The second line rounds the random number down so you get a random integer in the range of 0 to 5, inclusive (the legal range of indexes into the array).

Dan

| **Re: Explain this function?** on May 7, 2012 at 6:13:03 pm |

Hi Dan,

Thanks for the response and explanation, I really appreciate it!

Cheers,

Chris