Update: Flash is now more or less a dying technology, I'd strongly recommend using a JavaScript alternative.

It's quite often that I need to generate a random integer in an ActionScript application. Surprisingly, there is no function for this already in the AS3 library. So, I made a little function that'll do the job:

function getRandomNumber(min:int = 0, max:int = 0)
{
    if(min > max)
    {
        throw new Error('Random Number: min must be less than max');
    }
   
    var random:int = Math.round(Math.random() * (max - min)) + min;
   
    return random;
}

Here, the random number will fall between the first and second parameter. A few examples:

trace(getRandomNumber(1, 10)); //Prints random integer between 1 and 10
trace(getRandomNumber(5, 10)); //Prints a random integer between 5 and 10
trace(getRandomNumber(15, 10)); //Throws an error

Math.random() generates a decimal between 0 and 1. So, if we multiply this fraction by the difference between are largest and smallest number, we will insure that the number will not be greater then our largest number. After rounding the number to the nearest integer, we will add our minimum number to the integer. This insures that our randomly generated integer is at least our lower number.