|
||||||
|
||||||
Click by Click: Generating Random Numbers
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
Step Two: Setting Up the Text Box
The Text box is used to display the random number.
Step Three: Setting Up the Button ActionScript
Important Note: In the top left corner of the Actions Panel it must say: Actions - Button
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.
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
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
2627 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.
|