Random Numbers in ActionScript

ActionScript’s Math class provides methods for generating pseudorandom numbers. Here’s an example of how to use them:

package {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class RandomNumbers extends Sprite {
        public function RandomNumbers() {
            var output:TextField = new TextField();
            output.width = 400;
            output.height = 300;
            addChild(output);

            // Generate a random integer between 0 and 99
            output.appendText(Math.floor(Math.random() * 100) + ",");
            output.appendText(Math.floor(Math.random() * 100) + "\n");

            // Generate a random float between 0 and 1
            output.appendText(Math.random() + "\n");

            // Generate random floats in the range 5.0 <= f < 10.0
            output.appendText((Math.random() * 5 + 5) + ",");
            output.appendText((Math.random() * 5 + 5) + "\n");

            // Use a custom random number generator with a seed
            var customRandom:CustomRandom = new CustomRandom(42);
            output.appendText(customRandom.nextInt(100) + ",");
            output.appendText(customRandom.nextInt(100) + "\n");

            var customRandom2:CustomRandom = new CustomRandom(42);
            output.appendText(customRandom2.nextInt(100) + ",");
            output.appendText(customRandom2.nextInt(100) + "\n");
        }
    }
}

class CustomRandom {
    private var seed:Number;

    public function CustomRandom(seed:Number) {
        this.seed = seed;
    }

    public function nextInt(max:int):int {
        seed = (seed * 1103515245 + 12345) & 0x7fffffff;
        return seed % max;
    }
}

In this example, we’re using ActionScript’s built-in Math.random() function to generate random numbers. This function returns a pseudorandom number between 0 (inclusive) and 1 (exclusive).

To generate random integers, we multiply Math.random() by the desired range and use Math.floor() to round down to the nearest integer.

For generating random floats in a specific range, we multiply Math.random() by the range size and add the minimum value.

ActionScript doesn’t have a built-in seeded random number generator, so we’ve implemented a simple custom one (CustomRandom class) to demonstrate the concept of seeded randomness. This custom generator uses a linear congruential algorithm, which is not as robust as more modern algorithms but serves to illustrate the principle.

When you run this code, you’ll see output similar to this:

68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49

Note that the actual numbers will be different each time you run the program, except for the last two lines which use the seeded custom random number generator.

Remember that the random numbers generated by Math.random() are not suitable for cryptographic purposes. For applications requiring high-quality random numbers, you should use a more sophisticated random number generator.

ActionScript’s random number generation capabilities are more limited compared to some other languages, but they’re sufficient for many common use cases in game development and interactive applications.