A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Random SWF

  1. #1
    Junior Member
    Join Date
    May 2001
    Posts
    8

    Random SWF

    Hi,

    I'm having trouble searching the forum (using Safari), so I will ask my question and apologize if it's been answered.

    I want to get a random number, but then exclude one of those numbers. How do I do that? Here is my script:

    random = Number(random(5))+1;
    loadMovieNum("q_" + random + ".swf", 1);

    Let's say I want to exclude the number 2 from being chosen. What can I do to that script to make sure it only chooses 1, 3, 4 and 5?

    Thanks,

    Yevri

  2. #2
    You again?!?!?! gunko2's Avatar
    Join Date
    Jun 2004
    Location
    Israel
    Posts
    171
    maybe:
    code:

    random = Number(random(5))+1;
    if(random != 2){
    loadMovieNum("q_" + random + ".swf", 1);
    }


    just a thought....
    It ain't broke, it just lacks duct tape.

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here are two methods:

    1.
    code:

    i = random(4)+1;
    if (i >= 2) i++;



    2.
    code:

    choices = [1,3,4,5];
    i = choices[random(4)];



    * * *

    There is also a very elegant way of solving the problem of choosing N numbers from a larger set of M numbers. Search the boards for 'donald knuth' to find some previous threads where I've posted this algorithm.

    * * *

    Some other notes:

    A couple of problems with this line:

    random = Number(random(5))+1;

    It is best not use variables which have the same names as built-in functions. Instead of calling your variable 'random', try calling it 'r' or 'rnd'.

    Secondly, your use of the Number() function isn't necessary - random(5) already returns a number.

    r = random(5)+1;

  4. #4
    Junior Member
    Join Date
    May 2001
    Posts
    8

    Random Movie

    Thanks! That works perfectly. I appreciate you taking the time.

    Yevri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center