Flash CS3 Tutorial on Random Numbers
Links to some Great Flash CS4 Tutorials
Flash CS4 Tutorial, Random Numbers
Here are a few links to more great Flash CS4 Tutorials
- Flash CS4 Tutorials, Home Page
- FrenchSquared, is a website dedicated to teaching Flash CS4 and Actionscript. You will find all kinds of Flash CS3 Tutorials, Flash CS4 Tutorials and How to Actionscript Guides.
- Flash CS4 Tutorials
- List of Flash CS4 Tutorials, ActionScript 3.0 Tutorials and Flash Actionscript how to guides
- Flash Actionscript Tutorial, Arrays
- Flash CS4 Tutorial on how create and use Arrays
- Flash Actionscript 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 CS4 Tutorial, Source Files
Flash CS4 Tutorials, Copyright FrenchSquared 2008
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 CS4 Tutorial, Source Files
Flash CS4 Tutorials, Copyright FrenchSquared 2008
Great Flash Stuff on Amazon
Flash and Actionscript stuff from Amazon
Looking to learn flash and actionscript then check out this great stuff from Amazon
There was an error connecting to the Amazon web service. Please try again. Sorry, there are no results available from Amazon.
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)
Gordon French
Flash Tutorials
F2-4Kids.com.com
IT Admin
CSS How To (more)




