Flash Random

random

Availability
Flash Player 4. This function is deprecated in Flash 5; use of the Math.random method is recommended.


 

Usage
random( value )

Parameters
value An integer.

Returns
An integer.

Description
Function; returns a random integer between 0 and one less than the integer specified in thevalue parameter.

Example
The following use of random returns a value of 0, 1, 2, 3, or 4:

 

 random(5);

 

Usage

 Math.random() 

Parameters
None.

Returns
A number.

Description
Method; returns n, where 0 <= n < 1.

See also
random

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 rounddown 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()*(y-x))+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 y-x=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. =)

 

 

this is not code, just want the alignment

random(6)                     => 0 - 5

Math.random()                 => 0 - .9999

Math.random()*6               => 0 - 5.9999

Math.floor(Math.random()*6)   => 0 - 5 (integers)

Math.floor(Math.random()*6)+1 => 1 - 6 (integers)
Note that the +1 happens after the floor.

Math.ceil(Math.random())      => 0 - 1 (integers) 
with a very-very low chance of getting zero

Math.ceil(Math.random()*6)    => 0 - 6 (integers)
with a very-very low chance of getting zero

Leave a comment