/ Published in: ActionScript 3
Generating a random number is often useful when developing applications in AS3 (more so when developing games). AS3 has a built in random number method as a part of it's Math class which generates a number less than 1 and greater than or equal to 0. We can use this to create a useful function for generating a random number between two other numbers.
Expand |
Embed | Plain Text
function random(min:Number, max:Number):Number { return min + Math.random() * (max - min); }
Comments
Subscribe to comments
You need to login to post a comment.

Awesome! If you're looking for an awesome suite of Utilities. The Casa Library (CasaLib.org) rocks! For example, they offer your example NumberUtil.randomIntegerWithinRange( minimum, maxium ) as well as many variations such as finding an average, rounding up/down, etc. http://as3.casalib.org/docs/index.html?http://as3.casalib.org/docs/package-orgcasalibmath.html#Classes