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.

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.
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.

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.
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.
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.
Egypt Urnash says:
Hey, thanks a lot! I’m in the final stages of debugging a little game and this (along with a simple input logger/playback routine) is exactly what I needed to track down the last few hard-to-evoke bugs.
Noel says:
Awesome. It’s always good to hear when someone finds this stuff useful.
LolitochkaBC says:
Ааану-ка ребятки голосуем!!!
Признавапйтесь проказники и влаедльцы сапйта http://www.dncompute.com ))))
ЧТО вы будете делать этми летом?!
baron says:
Hi… my name is baron and i’m a brazilian designer… your work is really… really good…
i hope see more fantastics work from you.
sorry my bad english
Anze says:
Hi!
First of all, thanks for sharing - I haven’t used the code yet but I intend to. I wholeheartedly agree with all of the points you make and I couldn’t believe that Macromedia / Adobe decided that seeding the randomizer is not useful enough to be included in the player.
Anyway, the license on your files is this:
* This class is licensed under Creative Commons Attribution 3.0 License:
* http://creativecommons.org/licenses/by/3.0/
* Essentially you are free to use this class in any way you want, but anywhere
* you list credits for a work which uses this class you need to also acknowledge
* this classes author.
*
* @author Noel Billig (http://www.dncompute.com)
I am not accusing you of anything (you do acknowledge the original author in the article), I just think it would be nice if you mentioned the original author in the license too. Translating the algorithm still doesn’t make it completely your work and he does state “I do ask that you keep the comment intact”:
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (paul@honeylocust.com)
// See: http://www.honeylocust.com/javascript/randomizer.html
Anyway, thanks for sharing the code! Hope I’m not being a pain in the a**… ;)
Best,
Anze
Anze says:
Please delete my previous post - I see you _do_ mention the original author. :)
Thanks,
Anze
Tom says:
Great stuff - thanks for sharing, this stuff has been useful to me - brilliant work..
simplebits.org » Seeded random in ActionScript 3 says:
[...] SeededRandomizer. A Seed Based Pseudorandom Number Generator in Actionscript by Noel Billig. Posted at Apr 12, 2008 - 7:12 pm by admin in UncategorizedFollow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site. [...]