 Home
 Tutorials
 Flash
 Intermediate
 Maths Functions including Random
Page 1 of 1
Dennis Bale
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Dennis BaleWritten by: Dennis Bale
Time: 1520 Minutes
Difficulty Level:Intermediate
Requirements: Flash 5
Assumed Knowledge: Basic Flash actionscript use, using the actions window, variables, creating symbols and textboxes.
Download the source(PC ZIP File).
The random function has many uses in Flash, generating basic random numbers, creating random movement, creating random colors and many others. The example above creates a random integer between 100 and 100 everytime you click the button. This tutorial will cover creating the example, and also how to incorporate the random function to achieve different results.
The basic random function is as follows:
Math.random();
This creates a random number between the values of 0.0 and 1.0. For example: 0.0105901374530933 or 0.872525005541986. Several other functions can now be used to change the number that is created for better use in your movie. These include:
Math.round();
Math.ceil();
Math.floor();
These Math functions all round a number so that it becomes an integer, or whole number. The Math.round(); function will round up or down the number or expression that is inside its brackets to the nearest whole number. The Math.ceil(); function will do the same, however it will always round up to the nearest whole number. The Math.floor(); function is the opposite of the Math.ceil(); , it will always round down to the closest whole number.
To incorporate these with the random function, you can do this:
Math.round(Math.random());
This expression will now create a random number between 0.0 and 1.0, and then round it up to the closest whole number. So in this case, the number created will always equal either 0 or 1. This expression is useful for creating a 50% chance of an event occuring, ie flipping a coin. or for use with booleans (true/false statements).
To create a random number between the values of, say 0 and 10, you could do this:
Math.round(Math.random()*10);
The *10 multiplies your random number by 10, then it is rounded by the Math.round function to an integer. To create a random number between 1 and 10, you could do this:
Math.ceil(Math.random()*10);
This way the random number is always rounded up, and cannot equal 0. Another procedure is to create random numbers between two numbers other than 0 and something else. To create a random number between 5 and 20, do this:
Math.round(Math.random()*15)+5;
Or in other words, to create a random number between the values x and y:
Math.round(Math.random()*(yx))+x;
This will hold true for all values of x and y, ie they can be negative numbers as well as positive.
To make the example above, first create a new button, and place an instance of it onto the main stage. Create a new Dynamic text box, and give it the variable name of "display". That's the creation out of the way, and we can now do the scripting.
Select your button, and then open the actions window. You can either manually insert the following code, using the dropdown menus, or just copy and paste for now. Insert the code:
on (release) {
display = Math.round (Math.random ()*200)100;
}
And that's it! Now test your movie and give it a try out. You'll notice that the values in the code follow the pattern set by the general formula ie if x = 100, and y =100, then yx=200 and +x = 100.
This can have many different applications within flash. For example, to have a movieclip appear in random positions on the screen between the values of 0 and 200 you could have the following. Note: movieclip instance is called "bob".
bob._x = Math.round(Math.random()*200);
bob._y = Math.round(Math.random()*200);
Or to set the size of a movieclip randomly (between 0 and 100):
bob._width = Math.round(Math.random()*100);
bob._height = Math.round(Math.random()*100);
Or to choose a random movieclip to load, say if our movieclips have a linkage name of "bob1", "bob2" and we have 5 of them:
i = Math.ceil(Math.random()*5);
attachmovie("bob"+i, "fred"+i, 1);
So that would attach a random movie, say "bob3", into level 1 and then give it an instance name of "fred3".
Well, that's the basics of using the random function in Flash 5. Hopefully now you will be able to incorporate this into your movies to produce some even more amazing flash work. =)
Spread The Word
Related Articles
14 Responses to "Maths Functions including Random"
said this on 27 Oct 2008 9:39:06 PM EDT
This method is great most
To get zero fo 0 < Whereas to get a one i 0.5 < x < 1.5 (a ran Better to u 
said this on 15 Nov 2008 12:50:38 PM EDT
thanks a lot, it's easier

said this on 19 Jan 2009 6:50:06 PM EDT
YOU GUYS ROCK!!! I was st

said this on 16 Apr 2009 12:36:26 AM EDT
As per first comment you
Alternativ 
said this on 30 Apr 2009 6:11:05 PM EDT
If you want positive numb

said this on 05 Aug 2009 7:04:08 PM EDT
how to make fix random nu

said this on 20 Apr 2010 7:05:48 PM EDT
Math.round(Math.random()
The above Math.floor(Math The abov Math.round(0 * 11 Math.round(1 * 11  0 Math.floor(1 * 11) = Th Math.floor(Math.ra T The other met 
said this on 05 Jun 2010 11:23:01 AM EDT
even distribution checked
works for all Integers  CLASS b  package public class RandomIn private var MIN: private var MAX:int public function Random public func MIN=p1>p2?p2:p1; M return } pr return Math.abs(p } } }  e  import R var my_rnd for(var i:uin trace(m } 
said this on 05 Jun 2010 7:04:45 PM EDT
distribution for "0" fixe
//CLASS by GH. pac public class Ran private v private var public functi MIN=p1>p MAX=p1==MIN?p2: return Math.floor(Ma } private fun i return Math.a } else { retur }}}} //example import RandomInteger; for trace(my_rnd.Rnd(12,3 } 
said this on 05 Jun 2010 8:30:29 PM EDT
finally... AS3.0 class...
package{ public class private private var public funct public function Rnd(p1:i MIN=p1> MAX=p1==MIN?p2 return Math.floor(M }}} //example va for(v trace(my_rnd.Rnd(12,33) } 
said this on 14 Jul 2010 3:08:52 AM EDT
does anyone know how to g

said this on 21 Oct 2010 1:35:59 PM EDT
"Math.floor(Math.random()
The above equatio M 
said this on 24 Jan 2012 8:53:49 AM EDT
How would you generate 'n

said this on 22 Nov 2012 9:09:01 AM EDT
@Rajesh
You should alway 