Using Random Numbers in Flash to create Randomization

One of my favorite things to do when creating a Flash site is to use random numbers to generate some fun randomization on the site.

You can create tons of cool randomness by generating a random number within a range. Here is the script I use. This is AS2 and AS3 friendly:


var high:Number = 10; //set the high number in your range here
var low:Number = 1; //set the low number in your range here

//Now generate the random number
var randomNum:Number = Math.round(Math.random() * (high - low)) + low;

//And trace it to make sure it is working
trace(randomNum);

I’ve used this with an xml file to load a random photo out of group of 10 or 20. Just grab the total number of nodes using .length and set that as your high variable.

Here is an example of that
http://www.jumpingjacktrailers.com/ (in the polaroid spot)

I’ve also used it to load a random movie clip or tell a movie clip on the stage to gotoAndPlay a random frame.

Here are a couple examples of that:
http://www.trickortreatstreet.us/
http://www.kassingandrews.com/

This keeps your site looking fresh, and also gets your site more visits/hits either by keeping users coming back, or by enticing them to hit refresh to see the randomization.

Hooray!

Error in my_thread_global_end(): 4 threads didn't exit