function rand(a:Number):Number{ return(Math.round(Math.random()*a)) }

Now, you can use the function to generate numbers from 0 to the entered number:

trace(rand(3)); // traces 0 or 1 or 2 or 3

If you need to set a specific interval of numbers to generate a random number from (for example, a number from 3 to 10), use this code:

function rand(min:Number, max:Number):Number { return(Math.floor(Math.random()*(max-min+1))+ min); }

And use it like this:

trace(rand(3, 6)); // traces 3 or 4 or 5 or 6

And that's how you generate random numbers in Actionscript 3!

## No comments:

## Post a Comment