Flash random()

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:

  1. function randRange(min:Number, max:Number):Number {
  2.     // Generate a number between and including 1 – 5
  3.     var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
  4.     return randomNum;
  5. }
  6. // Get random number created
  7. var n:Number = randRange(1, 5);
  8. // Load image into holder on the stage. images should be numbered sequentially
  9. holder.loadMovie("image"+n+".jpg");
Download random load image flash Version 0.1

This example go’s to a random frame on the timeline

  1. function randRange(min:Number, max:Number):Number {
  2.  // Generate a number between and including min – max
  3.  var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
  4.  return randomNum;
  5. }
  6. //build array to hold frame lable names
  7. var frameArray:Array = new Array();
  8. frameArray[0] = "animate1";
  9. frameArray[1] = "animate2";
  10. frameArray[2] = "animate3";
  11. frameArray[3] = "animate4";
  12. frameArray[4] = "animate5";
  13. // Get random number created
  14. var n:Number = randRange(0, frameArray.length);
  15. // gotoAndStop Frame lable name
  16. gotoAndStop(frameArray[n]);
Download Goto random frame Version 0.1

This example will attach a random link movieClip from the library onto the stage.

  1. function randRange(min:Number, max:Number):Number {
  2.  // Generate a number between and including 1 – 5
  3.  var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
  4.  return randomNum;
  5. }
  6. // Get random number created
  7. var n:Number = randRange(1, 5);
  8. // Load movieClip into holder on the stage
  9. holder.attachMovie("img"+n,"img"+n,holder.getNextHighestDepth());
This entry was posted in Flash general, php, swx and tagged , , , , . Bookmark the permalink.

28 Responses to Flash random()

  1. Liam R says:

    Me ruv you rong time too!

  2. ^me took long time to reply me no lov you no more
    20 dollas now! no free love

  3. Pingback: Flash random anything | rapid-DEV.net

  4. Joe K says:

    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

  5. Yes it is possible
    var n:Number = randRange(2, 30);

  6. Rob says:

    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

  7. Sorry i have no experience with flash for mobile devices.

  8. Sky says:

    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!

  9. Cool, what’s the question?

  10. Sky says:

    How would I go about calling certain symbols (movie clips) randomly from the library and placing them on the stage where I want them?

  11. mark says:

    hey paul, nice tut, but how is done in AS3?
    thanks a lot, great site full of info!

  12. ? All 3 examples will work with AS3 with only minor modification. Can you be more specific in your question please.

  13. mark says:

    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!

  14. mark says:

    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.

  15. Come on! my 7 years says i dont get it. lol
    Do you know AS2 or AS3?

  16. mark says:

    a bit of as3, but not much, if it’s too much trouble no problem, i’ll figure it out. Thanks anyway, Paul

  17. Roy says:

    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

  18. 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.

  19. Roy says:

    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]

  20. 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)

  21. Marty says:

    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?

  22. There is no difference in the coding structure between AS2 and AS3 version for going to a random frame. ;)

  23. Nick says:

    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.

  24. Paul Ferrie says:

    Can you post all your code or email me over your fla?

  25. Nick says:

    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());

  26. Paul Ferrie says:

    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

  27. Nick says:

    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.

  28. Pingback: redirect to new url when flash video ends - SitePoint Forums