FREE Flash CS3 Tutorials
Flash Tutorial showing you how to Generate Random Numbers and perform basic math such as addition and subtraction

You will find Flash CS3 Tutorials, Flash Tutorials, and Lots of General FREE Flash tutorials.

Index.html
Flash_Concentration.html
new_in_3.html
what_is_AC.html
history_of_AC.html
AC_index.html Flash CS3 Tutorial Naming Basics
Flash CS3 Tutorial DragAndDrop
Flash CS3 Tutorial SoundBasics
Flash CS3 Tutorial Loops
http://www.f2-4kids.com

ActionScript

Flash CS3 Tutorial: Random Numbers

StumbleUpon Toolbar


Random Numbers
Flash has built in methods that can generate random numbers. Math.random() will generate a random number similar to 0.3048873310908675. There isn’t much you can do with such a number so if you add Math.floor() to the random method flash will generate the number 0. The Math.floor method rounds the number inside the parentheses to the lowest whole number, and since math random only generates a number less then zero, the number produced will always be zero. Flash is prepared for this. Yet, there is one more thing that needs to be done. Multiply the random number by a whole number. So, the new equation should resemble Math.floor(Math.random() * 5). This equation will generate a random number between 0 and 4, if you want 1 -5 you simple add 1 to the end of the equation. The multiplier is the key, if you want to generate a random number between 1 and 99 you would simple change the 5 to 99 and add 1 to the end of the equation.


open the Start01 file and select the first key frame on the actions layer and press F9 to open the Actions Panel and add the example code. This code is creating 4 variables r1 - r4, then Flash is setting each one of the four variables to be equal to a whole number ranging from 0 - 9.

var r1 = Math.floor(Math.random() * 9)
var r2 = Math.floor(Math.random() * 9)
var r3 = Math.floor(Math.random() * 9)
var r4 = Math.floor(Math.random() * 9)

In the source file you will see that several text field have been created for you. If you need information on how to create a dynamic text field, please see the Flash CS3 Tutorials in the text section of this web site. Four of the text field have been named r1_txt - r4_txt. The four lines of code in the example simply place the random number created above into the four text fields.

r1_txt.text=r1
r2_txt.text=r2
r3_txt.text=r3
r4_txt.text=r4

Next, something needs to be done with the random numbers. This next section of example code simple creates two more variables a1 and a2. Then a1 is set to equal the sum of random number 1 and random number 2. Flash can work simple math problems the same as any calculator. Typing 1 + 2 will result in three. So, that means the equation to add two of the numbers together and the equation to subtract two of the numbers has been created. The only thing left to do is to display the answers of the two equations.

var a1 = (r1 + r2)
var a2 = (r3 - r4)

The last example code places the a1 and a2 inside the text fields a1_txt and a2_txt.

var a1 = (r1 + r2)
var a2 = (r3 - r4)


Press Control-Enter and you will see Flash Generate 4 random numbers, work a mathematical Continue Smileystage and randomly changes the y position.


Download Flash CS3 Tutorial, Source Files

Flash CS3 Tutorials, Copyright FrenchSquared 2008
ActionScript