Recipe 4.7. Generating a Random Number


Problem

You want to use ActionScript to generate a random number.

Solution

Use Math.random( ) to generate a random number between 0 and .999999. Optionally, use the NumberUtilities.random( ) method to generate a random number within a specific range.

Discussion

You can use the Math.random( ) method to generate a random floating-point number from 0 to 0.999999999. In most cases, however, programs call for a random integer, not a random floating-point number. Furthermore, you may want a random value within a specific range. If you do want a random floating-point number, you'll need to specify its precision (the number of decimal places).

The simplest way to generate random numbers within a range and to a specified precision is to use the custom NumberUtilities.random( ) method. This method accepts up to three parameters, described as follows:


minimum

The smallest value in the range specified as a Number.


maximum

The largest value in the range specified as a Number.


roundToInterval

The optional interval to use for rounding. If omitted, numbers are rounded to the nearest integer. You can specify integer intervals to round to integer multiples. You can also specify numbers smaller than 1 to round to numbers with decimal places.

The NumberUtilities class is in the ascb.util package, so be sure to include an import statement.


The following example illustrates some uses of the round( ) method:

// Generate a random integer from 0 to 100. trace(NumberUtilities.random(0, 100)); // Generate a random multiple of 5 from 0 to 100. trace(NumberUtilities.random(0, 100, 5)); // Generate a random number from -10 to 10, rounded to the  // nearest tenth. trace(NumberUtilities.random(-10, 10, .1)); // Generate a random number from -1 to 1, rounded to the  // nearest five-hundredth. trace(NumberUtilities.random(-1, 1, .05));

To test that the random numbers generated by the NumberUtilities.random( ) method are evenly distributed, you can use a script such as the following:

package {   import flash.display.Sprite;   import ascb.util.NumberUtilities;   import flash.utils.Timer;   import flash.events.TimerEvent;      public class RandomNumberTest extends Sprite {     private var _total:uint;     private var _numbers:Object          public function RandomNumberTest(  ) {       var timer:Timer = new Timer(10);       timer.addEventListener(TimerEvent.TIMER, randomizer);       timer.start(  );       _total = 0;       _numbers = new Object(  );     }          private function randomizer(event:TimerEvent):void {       var randomNumber:Number = NumberUtilities.random(1, 10, 1);       _total++;       if(_numbers[randomNumber] == undefined) {         _numbers[randomNumber] = 0;       }       _numbers[randomNumber]++;       trace("random number: " + randomNumber);       var item:String;       for(item in _numbers) {         trace("\t" + item + ": " + Math.round(100 * _numbers[item]/_total));       }     }        } } 

See Also

Recipes 4.3 and 4.11




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net