The standard way to generate a random number in Adobe Flash is to use the Math.random method. I’ve found Flash’s random number generator to be perfectly sufficient for all applications I’ve used it in up to this point. Unfortunately, the Math.random method does not allow you to “seed” the random numbers it generates; this is problematic if you want to generate the same random numbers repeatedly.

Dice Photo by Sarah Spaulding

Pseudorandom Numbers

For those of you unfamiliar with the details of random number generation, there are a few things which are important to know. First off, random numbers generated by software are not random, a mathematical algorithm is used to generate the numbers. This formula should generate an evenly distributed series of numbers. That is to say, if you use a generator to create a large list of random numbers from 1 to 10, the number 1 would appear about as often as the number 10 on the list. In fact, all of the numbers would appear approximately the same number of times. A poorly distributed list would show some sort of clumping. For example, if the number 2 showed up very frequently, as did its neighbors 1 and 3, while the number 9 rarely showed up, this would be a poorly distributed list. The numbers on the list should also appear in an indiscernible order, and the order of numbers should not repeat.

Mathematicians have devoted a vast amount of time coming up with many pseudorandom number generation techniques. In the worlds of science and cryptography, it is important that a random number generator avoids repetition at any point and meets a stringent set of criteria. Luckily, most of things people do in flash don’t require this type of mathematical rigorousness.


Typical random number algorithms will generate the same series of “random” numbers every time they are run. In order to generate a different series of random numbers, most algorithms provide you with the opportunity to provide a “seed” value. The seed is essentially a number that works as the starting point for the algorithm. If you provide different seeds, you will get a different series of numbers.

A lot of the languages I learned when I first started programming required you to seed the generator before you could even use it. To ensure the random numbers were different every time, you would commonly use the current time (in milliseconds since a predetermined date) as the seed. I always found this to be an unnecessary and somewhat intimidating step for new developers. Knowing how and why you need to seed a random number generator is a bit much for a young developer who just wants to simulate a roll of dice. Flash made it’s random number generator more usable for the vast majority of developers by not requiring a seed.

Why Seed?

In making the Math.random function easier to use, Adobe completely removed any ability to seed the Math.random method. This is a problem in situations where you might need reproducible random numbers. Debugging and testing are pretty obvious uses for seeded random numbers, but there are also some less obvious ones.

Galaga Image

I’m sure most of you are familiar with old school arcade games like Pac Man and Galaga which featured a seemingly endless series of levels. In Galaga, and numerous games like it, each level would feature a unique configuration of enemies. If you have only a few levels in a game, you can easily create some sort of data file to store the manually designed configuration of each level. However, in a game like Galaga, there could easily be a few hundred levels. Hand crafting each level and saving it into a data file would be incredibly time consuming, and possibly storage and load intensive.

An alternative is to create an algorithm to generate the levels. The generated levels could be saved to a file, or better yet, you could simply generate the levels each time the game is run and avoid saving a large configuration file altogether. A developer could use basic logic, combined with some randomness, to create an extremely varied collection of levels automatically.

This sounds like a good idea, but there’s a problem with it. If you generated your levels with a random function like Math.random, the levels will be different every time the game is played. This makes comparing high scores difficult because some players will receive levels that are easier than others. This is where seeded random number generation becomes useful. If you use the same seed every time, you can still use random numbers in your algorithm, but you can ensure that the results are identical for every user, every time they play. This means you can still generate levels with randomness, but also generate them the same way every time.

Actionscript Implementation

Adobe Flash doesn’t have a seed based random number generator as part of it’s core API. I searched for an algorithm that I could quickly implement for use in simple Flash experiments (it didn’t have to meet any cryptographic standards). Some googleing led me to Paul Houle’s “Central Randomizer” method. While the web page may be creaky (references to Netscape 2.0?), the randomize function is fantastically simple. It took only a few minutes to incorporate it into an actionscript class. Some quick tests showed satisfactory distribution and a lack of repetition in the small sets of numbers I would require.


I’ve created a visual demonstration of seeded random number generation. It’s a primitive piece of generative art which draws random squiggles on the screen. The file is about 3k and it will generate the exact same series of random squiggles on any computer, anytime it’s loaded. There is no saving and loading of the squiggles, it simply regenerates the same series of random numbers for each squiggle. Clicking on the movie will generate another random squiggle, while pressing any key will display a squiggle seeded from the key’s keycode. What should be noted about this demo is that the keys will always generate the exact same random squiggle by simply using the same seed for the random values.

Get It

The SeededRandomizer class and the RandomSquiggle example are packaged together. All code is written in AS2 and is released under CC Attribution License.

Edit 25 April 2007: It seems I'm not the only one to address Flash's lack of a seed based random number generator. Michael Baczynski presents a more academic approach to random number generation in actionscript.

Download Source