jump to navigation

PRNG A Seed Based Pseudorandom Number Generator in Actionscript September 2, 2010

Posted by Tournas Dimitrios in Actionscript.

The standard way to generate a random number in Adobe Flash is to use the Math.random method. Mostly the Flash’s random number generator is perfectly sufficient for all applications . 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.The following code demonstrates the use of the Flash.random method .

var max:Number = 10 ;
var min:Number = 1 ;
var randomNum:uint ;

for (var i:Number = 0; i < 10; i++){
 randomNum = Math.round(Math.random()*(max-min)+ min);



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.

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.

A good Pseudo-Random Number Generator (PRNG) :

When developing games you often need a PRNG that, once initialized with a seed value, produces always the same sequence of random numbers after it. The Math.rand() function in Flash is a PRNG, but it doesn’t let you define a seed value (I think it’s picked randomly when the swf starts, but there isn’t much information about it) so another solution has to be found.

After googleing I have found this PRNG .

Park-Miller-Carta Pseudo-Random Number Generator:
AS2 Version
AS3 Version

First define a seed value (default is one) and then call nextInt() to get the next integer value, nextDouble() to get a floating point number between 0..1 and nextIntRange() or nextDoubleRange() to generate numbers within a certain range.

import de.polygonal.math.PM_PRNG;

var pr:PM_PRNG = new PM_PRNG();
pr.seed = 123;

for (var i:Number = 0; i < 10; i++)


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s