2009. 12. 10. 14:31

Maths Functions including Random





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()*(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. =)