Random Number Generator

March 1, 2010 |  by  |  Project Three, Tutorials

I wanted to get something to play at a random delay time, so I found a function to generate a random number:

//To call the function, define lowest, and highest numbers you want to get (in this case it will return a random number between 1, and 5)

randomNumber(1, 5)

//Random Number Generator
function randomNumber(low:Number=NaN, high:Number=NaN):Number
{
var low:Number = low;
var high:Number = high;

if(isNaN(low))
{
throw new Error(“low must be defined”);
}
if(isNaN(high))
{
throw new Error(“high must be defined”);
}
return Math.round(Math.random() * (1+high – low)) + low;}

1 Comment


  1. Can you share exactly how you used this in your fla? The stand alone code may not make much sense to others.