|
||||||
|
||||||
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
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.
Step Two: Setting Up the Text Box
The Text box is used to display the random number.
The Variable Name is: myDisplay
Step Three: Setting Up the Button ActionScript
Note: If you take a button from the Common Button Library avoid Knobs, Faders and Component buttons as they work differently.
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:
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
on (release) {
myDisplay = random (10) +1;
}
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!
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
8113 visitors to this page since
2 July 07 •
|
|
|
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.
|