ActionScript for Flash MX: The Definitive Guide, Second Edition

ActionScript for Flash MX: The Definitive Guide, 2nd EditionBy Colin Moock
Chapter 18.  ActionScript Language Reference
Math.random( ) Method Flash 5; can be used when exporting Flash 4 movies

generate a random number from 0 to 1.0

Math.random()

Returns

A floating-point number greater than or equal to 0.0 and less than 1.0.

Description

The random( ) method provides a way to produce random numbers, which can then be used to choose randomly between actions in a script. The random( ) method generates a random value between 0 and .99999..., inclusive, which we can scale according to our needs. For example, to obtain a random number between 0 and 5, we use:

Math.random() * 5

And to obtain a random integer between 1 and 6, we use:

Math.floor(Math.random() * 6) + 1

This custom function returns an integer number in a specified range rather than a floating-point number in the range 0 to 1:

// Returns a number in the range of minVal to maxVal, inclusive function myRandom (minVal, maxVal) { return minVal + Math.floor(Math.random() * (maxVal + 1 - minVal)); } // Invoke the function dieRoll = myRandom(1, 6); // Emulates a six-sided die trace(dieRoll); // Note that to simulate two dice, you can use this: twoDice = myRandom(2, 12); // The minimum value is 2, not 1 // To return the die values separately, use an array function rollTwoDice () { return [myRandom(1, 6), myRandom(1, 6)]; }

Due to a bug in Flash Player 5.0.30.0, this approach is prone to an extremely rare, but potentially important inaccuracy. In Flash Player 5.0.30.0, random( ) generates values in the range of 0.0 to 1.0, inclusive. When we multiply the return of random( ) by an integer, n, we produce values in the range of 0.0 to n. In our example, we multiplied Math.random( ) by 6, so that the returned value ranges from 0.0 to 6.0. By invoking floor( ) on the adjusted value, we produce integers in the range of 0 to n (0 to 6 in our example). The scaling and rounding leads to an inaccurate distribution of random numbers the chance of producing n is much smaller than the chance of producing any other number in the series.

The Math.random( ) was fixed in Flash Player 5.0.41.0. If supporting older versions of the Player, the following version of the myRandom( ) function avoids the problem by simply discarding the value 1.0 if it happens to be chosen by Math.random( ):

// Returns an integer in the range of minVal to maxVal, inclusive function myRandom (minVal, maxVal) { do { r = Math.random(); // Keep picking a number until it is not 1. } while (r = = 1); return minVal + Math.floor(r * (maxVal + 1 - minVal)); } // Invoke the function dieRoll = myRandom(1, 6); // Emulates a six-sided die safely in Flash 5.0.30.0

Usage

Math.random( ) replaces the deprecated Flash 4 random function.

Example

Math.random( ) is often used to cause the playhead to jump to a random frame in the timeline. The following code invokes the myRandom( ) function from the preceding example and then sends the playhead to the randomly chosen frame:

// Invoke the function; pick a random number between 10 and 20 var destinationFrame = myRandom(10, 20); // Now send the playhead to that frame gotoAndStop(destinationFrame);

    Категории