From time to time i come across post’s on the Flash Developer forums asking about how to attach or load a random image/object on to the flash stage. If you check the flash doc’s you will see that there is already a neat little function there already for use. For those that have no idea what a flash manual is or how to find the flash doc’s(F1) here are a couple of examples.
This example loads a random image into flash:
-
function randRange(min:Number, max:Number):Number {
-
// Generate a number between and including 1 – 5
-
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
-
return randomNum;
-
}
-
// Get random number created
-
var n:Number = randRange(1, 5);
-
// Load image into holder on the stage. images should be numbered sequentially
-
holder.loadMovie("image"+n+".jpg");

This example go’s to a random frame on the timeline
-
function randRange(min:Number, max:Number):Number {
-
// Generate a number between and including min – max
-
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
-
return randomNum;
-
}
-
//build array to hold frame lable names
-
var frameArray:Array = new Array();
-
frameArray[0] = "animate1";
-
frameArray[1] = "animate2";
-
frameArray[2] = "animate3";
-
frameArray[3] = "animate4";
-
frameArray[4] = "animate5";
-
// Get random number created
-
var n:Number = randRange(0, frameArray.length);
-
// gotoAndStop Frame lable name
-
gotoAndStop(frameArray[n]);

This example will attach a random link movieClip from the library onto the stage.
-
function randRange(min:Number, max:Number):Number {
-
// Generate a number between and including 1 – 5
-
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
-
return randomNum;
-
}
-
// Get random number created
-
var n:Number = randRange(1, 5);
-
// Load movieClip into holder on the stage
-
holder.attachMovie("img"+n,"img"+n,holder.getNextHighestDepth());

Me ruv you rong time too!
^me took long time to reply me no lov you no more
20 dollas now! no free love
Pingback: Flash random anything | rapid-DEV.net
Thanks for this wonderful post! It was exactly the information I needed to start a project. I’m new to AS3 and revisiting Flash for the first time since Flash 5… so it’s been a while. I did have a question in regards to the “random frame” example. I have a 30 frame time line and I would like the random function to exclude frame 1 from being a possible result… thus leaving the random function to only select frames 2-30. Is this possible? I believe it has to do with the randRange(min:number, max:number) but I’m not 100%… Am I even warm?
-Joe K
Yes it is possible
var n:Number = randRange(2, 30);
Hi, nice stuff… Could you do this for flash lite 1.1? Im trying to make a spin the bottle game for a range of mobile phones, and I need a random timeline stop, but for the “function” command It needs to be flash 5 (apparently cant use 5 for most cell phones). Thanks in advance
Sorry i have no experience with flash for mobile devices.
I am pretty new to Flash but this sounds like this is in the ball park of what I am needing. I am trying to develop a Flash language program for children ages 3-4. So far it is set up nicely where a child can click on a number, color, or a few animals and hear a translation. What I would like to do is be able to test a child by having random multiple choice questions (3 choices) where the audio is heard and the child must click on the correct image. What I’d like it to do is have 3 separate categories (numbers, animals, and colors) and have it randomly pull from a specific category the correct answer along with 2 random incorrect answers. I’d really rather not use static pages for something like this since the children will eventually learn the pattern. Thank you for your time!
Cool, what’s the question?
How would I go about calling certain symbols (movie clips) randomly from the library and placing them on the stage where I want them?
hey paul, nice tut, but how is done in AS3?
thanks a lot, great site full of info!
? All 3 examples will work with AS3 with only minor modification. Can you be more specific in your question please.
hey thanks for the quick reply. I’m interested in the one pulling random clips form the library. I have 4 images in the library and i need to display one randomly. I have them inside movie clips and exported for actionscript. I put their names in the array but no go. Have you tried and it’s working in AS3? Do you have an example?
thanks a lot, paul!
I still didn’t get it, if you ever have any chance to get it working in AS3, please post it. So many of us will be greatful.
Thanks Paul.
Come on! my 7 years says i dont get it. lol
Do you know AS2 or AS3?
a bit of as3, but not much, if it’s too much trouble no problem, i’ll figure it out. Thanks anyway, Paul
Hello,
I tried with AS3 and i am getting this error.
TypeError: Error #1006: loadMovie is no function.
at index1as3_fla::MainTimeline/frame1()
Can you help me out with this thanks.
Roy
The answer is in the error. TypeError: Error #1006: loadMovie is not a function. loadMovie can only be used in AS2.
Please only ask questions that are relevant to what I have posted on the blog please.
This is the script:
[AS3]
function randRange(min:Number, max:Number):Number {
// Generate a number between and including 1 – 5
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
return randomNum;
}
// Get random number created
var n:Number = randRange(1, 5);
// Load image into holder on the stage
holder.loadMovie(“image”+n+”.jpg”);
[/AS3]
The code that I have posted is not AS3.
Replace holder.loadMovie… with the code below.
var ld:Loader = new Loader()
ld.load(new URLRequest(“image”+n+”.jpg”))
addChild(ld)
Hi, I’ve been searching around the web for code exactly like your “This example go’s to a random frame on the timeline” – index1b. Only it’s written for AS2, can you show how it would change for AS3?
There is no difference in the coding structure between AS2 and AS3 version for going to a random frame.
Hello, Thanks for the info, I am using the random link movieClip, now I want to use 55 clips instead of just 5 movieClips, I have changed the
var n:Number = randRange(1, 5);
to
var n:Number = randRange(1, 55);
yet it still just picks up the clips 1 to 5
Do I need to change another part of the script so that it incorporates all 55 movieClips or am I doing something else wrong. Can you please help.
Thanks.
Can you post all your code or email me over your fla?
Sorry here’s all the code.
function randRange(min:Number, max:Number):Number {
// Generate a number between and including 1 – 5
var randomNum:Number = Math.floor(Math.random()*(max-min+6))+min;
return randomNum;
}
// Get random number created
var n:Number = randRange(1, 55);
// Load image into holder on the stage
holder.attachMovie(“img”+n,holder.getNextHighestDepth());
why do you have +6 in there it should be +1. That’s to make sure you get a number between 1-55 not 6-61
Sorry that was something I was trying out to see if it would pick any of the movieClips over 5 but it still just picks/loads the first five movieClips randomly. forgot to change it back to a 1.
Pingback: redirect to new url when flash video ends - SitePoint Forums