First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Nuno Mira | Website:

Random Image

In order to generate random numbers in Flash you use Math.random(). This method returns a number x, where 0 <= x < 1.
Usually you want to generate a random integer n, where 0 <= n < max and max is also an integer.

This can bedone with Math.floor(Math.random() * max);

For example,

Math.floor(Math.random() * 5)

generates the numbers 0, 1, 2, 3 and 4.

More generally, Math.floor(Math.random() * (max - min)) + min generates a random integer n, where min <= n < max and min and max are also integers.

By testing the following piece of code you'll get the numbers 2, 3 and 4:


var max:uint = 5;
var min:uint = 2;

var rand = Math.floor(Math.random() * (max - min)) + min;

addEventListener(Event.ENTER_FRAME, traceRandom);
function traceRandom(e:Event)
	var rand = Math.floor(Math.random() * (max - min)) + min;

For the non-repetitive random image, you need to make sure the new number is different from the last one. For this I created the following recursive function which ensures that the random number is always different from n. Obviously, total has to be greater than 1, i.e., you have to have more than one image, otherwise this doesn't make sense, and this code explodes!

function randomNumber(n:int):uint
	var rand:uint = Math.floor(Math.random() * total);
	if (rand == n)
	return randomNumber(n);
	return rand;

This LSO has a different name - random - so that there are no conflicts with the previous example, but the rest of the code is practically the same. You just need to call randomNumber(). This is the code available in

// create a LSO identified by "random"
var lso:SharedObject = SharedObject.getLocal("random");

var current:uint;

// does "current" exist already			
if ( != undefined)
	// grab it
	current =;
	// create a new one, different from the previous
	current = randomNumber(current);				
// if it doesn't, create a new one
	// it can be any number
	// all random numbers are different from -1
	current = randomNumber(-1); 

// store current in the LSO = current;

// load the appropriate image

Everytime that you refresh the browser or test the movie, you'll get a different image from the previous one. In this case you can get image A, then B and then A again.

All the files are available for download here.

» Level Intermediate

Added: 2011-03-17
Rating: 3 Votes: 2
(10 being the highest)
» Author
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.