Flash CS3 Tutorial, Random Numbers

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 3 people | Log in to rate

Ranked #1,183 in Games, #39,791 overall

Flash CS3 Tutorial on Random Numbers

This lens is here to help you learn more about Flash CS3. This particular lens has a Flash Tutorial about creating random numbers and then performing a few math operations. You can find more of these great flash tutorials at

Links to some Great Flash CS3 Tutorials 

Flash CS3 Tutorial, Random Numbers

Here are a few links to more great Flash CS3 Tutorials
Flash CS3 Tutorials, Home Page
F2, is a website dedicated to teaching Flash CS3, You will find all kinds of Flash CS3 Tutorials
Flash CS3 Tutorials
List of Flash CS3 Tutorials and ActionScript 3.0 Tutorials
Flash CS3 Tutorial, Arrays
Flash CS3 Tutorial on how create and use Arrays
Flash CS3 Tutorial, Loops
ActionScript 3.0 Tutorial on how create loops : for, while, and do while.

Flash CS3 tutorial: Random Numbers 

Great Flash CS3 Tutorial

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

Great information about Flash 

Flash CS3 Tutorials, Random Numbers

Here is some great information I have found regarding Flash, and Flash Tutorials

Loading Fetching RSS feed... please stand by

by FrenchSquared

Gordon French is a Flash Programmer and Flash Designer. Check out My site at
Gordon French
Flash Tutorials
F2-4Kids.com.com
IT Admin
CSS How To (more)

Explore related pages

Create a Lens!