Accessibility

ActionScript cookbook beta

Create a random number within a range

Problem Summary

You need to create a random absolute number between 0 and 10.

Solution Summary

Use new Math APIs to generate a random number.

Explanation

ActionScript 2

var max:Number = 10;
var randInRange:Number = random(10 + 1);
trace(randInRange);

ActionScript 3

var rand:Number = Math.random();
var max:Number = 10;
var randInRange:Number = Math.round(rand * max);

trace(randInRange);

Note: The Math.random and Math.round functions are available in ActionScript 2, but are included here for completeness.