Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

# The importance of REAL random results

I was wondering if functions in various languages used to return random values, such as AS2 `Math.random()` or Php `rand()` are returning real random values or just a sequence of numbers someone can predict.

So I asked the question to someone that is facing this problem for a long time: Matt from CasinoInLinea.

That’s what he said:

Random results are very important in many computer applications. From online casino games, through video arcades, image processing to data security & encryption software and many other types of applications.

Since computers are 100% predictable: The same actions, performed at a specific order, will always lead to the same results. This means that generating true random numbers is not an easy task. For a function to produce a random number, one of the variables must have a different value every time.

A series of numbers is truly random if it completely unpredictable. In other words, if we have absolutely no way of knowing what the next number is in a series of N numbers, then the series is completely random. On the other hand, if we can determine the next number according to a specific formula, (i.e. Fibonacci, a linear or parabolic function etc), then the series is a function, and is not random at all. As odd as it may seem, computer functions also have interim statuses. A function can be highly random, (i.e. it’s difficult BUT POSSIBLE to predict the next number). It all depends on the capability of the RNG (random Number Generator) algorithm.

Applications like online casino software and security related programs (i.e. password generators, data encryption and more) demand the highest randomness possible. Imagine a roulette table in which a player can predict where the ball falls next, or a password generating software that generates predictable “random” passwords. Both scenarios present a major security breach for the software operators.

So how do random functions produce random numbers? Here are a few common ways:

The computer’s clock
That’s the easiest, cheapest and least secure method. The computer clock offers an ever changing value that always increases with time. Therefore, it is possible to integrate the absolute time value into the random function to produce random results. (The time value is a 2 * 16 bit, double word binary value) that contains years, months, days, hours, minutes & seconds).

Random keyboard or mouse sequence
Some applications (e.g. client-based password generators) resort to a more creative way of producing a random value. They ask the user to provide a sequence of keyboard strokes or a random mouse curosr movement over a specific screen area (or a combination of both). The keystrokes and/or mouse movements are then compiled to a series of values, which are then compiled to a single, random number. This method is excellent at giving the user a distinct sense of control. Its major drawback is that it cannot be used in automated processes (e.g. command line batch executions).

Hardware-based RNG
For applications where randomness is crucial, such as online casino software or encryption software, RNG extension cards can be installed on server machines. These offer faster, more secure random numbers, which are virtually inpredictable. When randomness is of outmost importance, Hardware based is the best solution, although the most expensive one.

Having said all that, when you develop your next computer game, be it browser or client based, you should not worry too much about random numbers â€“ feel free to use your development environment’s native random functions (which probably use the computer’s clock). The gaming experience is unlikely to be affected by it.

If you’re still serious about random numbers, and demand nothing but the best, this website provides more in-depth information about using random number genreators.

Rate this post: (16 votes, average: 3.75 out of 5)

## This post has 10 comments

1. ### Ben

on July 4, 2008 at 6:48 pm

Great post, E. I was wondering about this recently when making an AS3 flash game, whether using Math was enough. Is there a simple way to “seed” Math.random so that (as Matt said) “one of the variables has a different value every time.” I’ve been thinking a custom solution might actually cut down on processing time (which I’ve been needing somewhat in my game loop).

2. ### Mini Chris

on July 4, 2008 at 8:21 pm

ive always wondered how exactly they have a random function in flash cause its programmed into it which means it cant be random o.O

3. ### JDog

on July 4, 2008 at 8:57 pm

Wierd…

4. ### Kesh

on July 4, 2008 at 10:36 pm

I didn’t know they used time based random too… that was my idea… lol

5. ### Jack Hopkins

on July 5, 2008 at 12:16 am

I think the current percent of cpu and ram being used is also used to generate (somewhat) random numbers.
good post :)

6. ### Danny Miller

on July 5, 2008 at 2:04 am

Even more random are nature based random number generators. Check out the ones online that generate from lava lamps, radio waves, etc

7. ### Xodus

on July 5, 2008 at 2:27 pm

To make a random number in flash, they don’t actually use a pure random number, but a pseudo-random number. It is governed by a series of incredibally complicated equations which make it almost random. You can fid out more on wikipedia:

http://en.wikipedia.org/wiki/Pseudorandom_number_generator

8. ### Luiz Fernando

on July 6, 2008 at 1:49 am

My math teacher once said me that is IMPOSSIBLE to a computer generate a TRUE random number, becouse of a huge amount of factors. He also told me that a computer does not have a brain (something organic, that think emotionally), so it cannot do actions purely random, only mathematically stated actions.

9. ### Vinicius

on January 31, 2010 at 3:54 am

I w

10. ### Vinicius

on January 31, 2010 at 3:58 am

I was faced with the opposite problem. The flash Math.random function cannot be “seeded”, and I needed a peudo random function that will always return the same results given the same seed.

Fortunatly, gskinner came up with a solution in AS3 using the perlin noise seeded random function?

It’s all there :

http://www.gskinner.com/blog/archives/2008/01/source_code_see.html