Flash tutorial on how to Generating Random Numbers

Home • Members Tutorials Forum iSnapChat Contact Us 

 


Flash Tutorials

   

Click by Click: Generating Random Numbers

See Full Version: This is a shortened click by click version of a: Full Length Tutorial

 

 

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.



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. Click: OK
  5. 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. Select any Type setting that you might want: Font Styles


Step Three: Setting Up the Button
ActionScript

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

  6. If Script Assist is on, Switch it off:
  7. Type (or copy and paste) the following code in the Actions Panel;
  8. on (release) {
       myDisplay = random (10) +1;
    }

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

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:
   if (myNumber == myTemp) {
      // If it is repeating it generates a new random number:
      myNumber = random(mySetting)+1;
      // Continues as above:
      if (myNumber == myTemp) {
         myNumber = random(mySetting)+1;
         if (myNumber == myTemp) {
            myNumber = random(mySetting)+1;
            if (myNumber == myTemp) {
               myNumber = random(mySetting)+1;
            }
         }
      }
   }
// Displays the random number:
myDisplay = myNumber;
// Resets the variable to the new random number:
myTemp = myNumber;
}

 

 

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.

 

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 to see if it is repeating itself:
   if (myNumber == myTemp) {
      // If it is repeating it generates a new random number:
      myNumber = random(mySetting)+1;
      // Continues as above:
      if (myNumber == myTemp) {
         myNumber = random(mySetting)+1;
         if (myNumber == myTemp) {
            myNumber = random(mySetting)+1;
            if (myNumber == myTemp) {
               myNumber = random(mySetting)+1;
            }
         }
      }
   }
   // Displays the random number:
   myDisplay = myNumber;
   // 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!

This is a quick reference version of a full length tutorial: Full Length Tutorial


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