Learn Flash: Generate Random Numbers

Home • Members Tutorials Forum iSnapChat Contact Us 


Flash Tutorials


Flash Tutorial - Generating Random Numbers


Free Flash Tutorial


In this tutorials you will learn how to generate random numbers. The code for this is quite straight forward and has many uses. You might want to generate random numbers as an end in itself, such as a way of selecting a raffle ticket winner. But more often random numbers are used to trigger an effect. For example to display an image at random or create some other random action. In this tutorial you will learn how to convert the random number into a way of saying hello in a random language.


My Example: Download the Flash file  Int 039a

Click to generate a random number between 1 and 10.



Click by Click: If you would like to view this tutorial without all the notes: Click by Click

Step One: Setting Up the Document

  1. Open a New Flash Document: File > New (Ctrl N)
  2. If the General Tab is not selected, select it: General Tab
  3. Select Flash Document: Flash File Flash File (ActionScript 2.0)
  4. Important Note: The code in this tutorial will Not work in ActionScript 3.0 but if you are using an older version of Flash you can use ActionScript 1.

  5. Click: OK

  6. If you wish type something on Stage such as: Generate a Random Number between 1 and 10.

Step Two: Setting Up the Text Box

The Text box is used to display the random number.

  1. Select the: Text Tool
  2. On the Stage drag out a: Text Box
  3. Return to the standard: Selection Tool
  4. If the Property Inspector is closed, open it: Window > Properties (Ctrl F3)
  5. In the Property Inspector select: Dynamic Text
  6. If you wish select: Show Border Around Text
  7. For the Variable Name type: myDisplay

  8. The Variable Name is: myDisplay

  9. Select any Type setting that you might want: Font Styles

Step Three: Setting Up the Button

  1. If the Button Library is closed, open it: Window > Common Libraries > Buttons
  2. Go through the folder and select a button. I selected: Classic Buttons > Circle Buttons > Next
  3. Note: If you take a button from the Common Button Library avoid Knobs, Faders and Component buttons as they work differently.

  4. Drag a button on to the: Main Stage
  5. If the Actions Panel is closed, open it: Window > Action (F9)
  6. Important Note: In the top left corner of the Actions Panel it must say: Actions - Button

    If it does not say Actions - Button it can be for two reasons:

  7. If Script Assist is on, Switch it off:
  8. Note: With Script Assist on you cannot type in the Actions Panel. If you want to learn more about Script Assist see the tutorial on the Actions Panel

  9. Type (or copy and paste) the following code in the Actions Panel;
  10. on (release) {
       myDisplay = random (10) +1;

  11. Test your Movie: Control > Test Movie (Ctrl Enter)

The ActionScript Explained:

on (release) {
When the user releases the Mouse button what is between the curly braces ...

myDisplay = random (10) +1;
In this case the random function creates a random number that starts at 0 and ends on the 10th digit. Which is the number specified in the parentheses: (10). If you start your count from zero (instead of 1) then the 10th digit is 9 (not 10)! So to make it count form 1 to 10 I simply add one to the result. Hence the +1.

The curly brace ends the on (release) statement.

Step Four: Non Repeating Random Number

Most of the time you don't want a random number that is really random only nearly random! Mostly you want a random number which is non repeating. In other words every time you click on the button you want a new result not to repeat the number that is already there. Thus if my current number is 1 I want the next number to be any random number except 1.

The code that I am going to show you creates a random number and checks to see if it the same as the previous number that was generated. If it is the same it generates a new random number. Of course it could then generate the same number again so it checks this five times. Theoretically this could (and very occasionally will) generate the same number twice the statistical chance is very small. For example if i am generating a number between 1 and 10 the chance would be 1 in 100,000.

10 x 10 x 10 x 10 x 10 = 100,000

If I am generating a random number between 1 and 3 the natural repeat rate would be 1 in 3 or a very high chance. But if I run it through this simple system it increases to 1 in 243.

3 x 3 x 3 x 3 x 3 = 243

Try it and see how long you have to click before the number repeats (don't blame me if you get a sore click finger!!):

My Example: Download the Flash file  Int 039b

Click to generate a non repeating random number between 1 and 3.

The ActionScript attached to the button is as follows:

on (release) {

   // Sets the number of digits in the random sequence:
   var mySetting = 3;

   // Creates a random number:
   myNumber = random(mySetting)+1;

   // Checks to see if it is repeating itself:

   while (myNumber == myTemp) {
      myNumber = random(mySetting)+1;
   // Displays the random number:
   myDisplay = myNumber;
   // Resets the variable to the new random number:
   myTemp = myNumber;

With Thanks: Code submitted by Alexander Kuzmin

The mySetting = 3; sets the number range. In this case it will produce a random number between 1 and 3. The variable myTemp holds the previous generated random number.



Step Five: Converting the Random Number to Text


In general you do not want to create a random number for the sake of creating a random number. You create a random numbers to create some other random event. Once you have created a random number it is actually quite easy to convert it to something useful. In this example I convert the number into text. Each number is associated with a particular word (or words). In this case the word hello in 10 different languages:


My Example: Download the Flash file  Int 039c

Click to say hello in 10 different languages.

Note: If you can guess all 10 languages I will eat my computer (just joking) !!

The ActionScript in first section is identical to the previous example except that I have changed mySetting to 10 as I want to generate a random number between 1 and 10. The second converts the random number to text. I hope the gray comments make the code self explanatory:

Note for AS2: The code below will work in AS1 or AS2 but if you are using AS2 there is a shorter way to achieve the same thing. You can use an Array: See sample code

The ActionScript attached to the button is as follows:

on (release) {

// Sets the number of digits in the random sequence:
var mySetting = 10;
// Creates a random number:
myNumber = random(mySetting)+1;
//Checks the repeat function
while (myNumber == myTemp) {
myNumber = random(mySetting)+1;
// Resets the variable to the new random number:
myTemp = myNumber;

   // This section converts the number to text.
   // Each number is associated with a particular sentence.
   // On this occasion my random number is from 1 to 10.

   if (myNumber == 1) {
      myDisplay = "Hello";
   } else if (myNumber == 2) {
      myDisplay = "Salut";
   } else if (myNumber == 3) {
      myDisplay = "Guten Tag";
   } else if (myNumber == 4) {
      myDisplay = "Hola";
   } else if (myNumber == 5) {
      myDisplay = "Dobrý Deň";
   } else if (myNumber == 6) {
      myDisplay = "Nei Ho";
   } else if (myNumber == 7) {
      myDisplay = "Ciao";
   } else if (myNumber == 8) {
      myDisplay = "God Dag";
   } else if (myNumber == 9) {
      myDisplay = "Namaste";
      // I don't have to specify the last digit:
   } else {
      myDisplay = "Kia Ora";

That's it. Enjoy being random!

I hope you have found this useful. If so perhaps you could recommend this site to others and link to webwasp!


Please indicate what you thought of this tutorial 
10 is the best: 
10 9 8 7 6 5 4 3 2 1

30098 visitors to this page since 2 July 07 •

Phil Schulz's Facebook Profile
Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend: Click here

 Top of Page HomeMembers Tutorials Forum iSnapChat Contact Us 
 All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.