A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: random numbers

  1. #1
    Member
    Join Date
    Aug 2004
    Location
    China
    Posts
    43

    random numbers

    how to pick out 5 random different numbers from 22 numbers?

  2. #2
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    I answered your question on actionscripts.org ...

    http://www.actionscripts.org/forums/...d.php3?t=52380

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here:
    myNumber = [];
    for(m=0;m<=4;m++){
    myPick = Math.round(random(22));
    pushed = myNumber.push(myPick);
    if(pushed>=5){
    for(i=0;i<=4;i++){
    trace(myNumber[i]);
    }
    }
    };
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    If it was that easy my friend!

    Problem with your above code, is that you'll often get some same 2 numbers (and if you were lucky enough you could get the same 5 numbers!), and that basically means you're not allways getting 5 "unique random" numbers!
    You must check if in the generated numbers if there are no duplicates, and generate new different numbers, if there are some duplicates.

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here is an elegant way to solve this problem that doesn't require futzing with arrays. I first read about this years ago in Jon Bentley's "Programming Pearls" column. The algorithm is by the great Donald Knuth.

    It very careful sets up the odds so that each number has the correct statistical chance of being selected.

    One of the nice side effects of this algorithm is that the numbers are produced in sorted order. It can be used to select from arrays of other objects as well, such as image-names, frame-labels etc.


    code:

    // Choose N numbers from a list of M numbers (ranging from 0 - M-1)

    M = 22;
    N = 5;

    for (i = 0; i < M && N > 0; ++i)
    {
    if (random(M-i) < N)
    {
    trace(i);
    N--;
    }
    }



    If you wanted to stick these numbers into an array, you could do it this way:

    code:

    // Choose N numbers from a list of M numbers (ranging from 0 - M-1)

    M = 22;
    N = 5;
    ary = [];

    for (i = 0; i < M && N > 0; ++i)
    {
    if (random(M-i) < N)
    {
    ary.push(i);
    N--;
    }
    }
    trace(ary);



    Finally, if you want the numbers to start from 1 (to be in the range 1-M) then change this line:

    ary.push(i);

    to this:

    ary.push(i+1);

  6. #6
    Member
    Join Date
    Aug 2004
    Location
    China
    Posts
    43
    thank you very much.

    //I can not understand the actionscript very well and have some questions,help me please,thanks advance.
    // Choose N numbers from a list of M numbers (ranging from 0 - M-1)

    M = 22;
    N = 2;
    for (i=0; i<M && N>0; i++) {//why N>0 ? N =2,2 >0.is it unnecessary?
    if (random(M-i)<N) {
    trace("M is:"+M);//output "M is:22"
    trace("M-i is"+(M-i));//output "M-i is7",
    trace("random M-i is:"+random(M-i));//output "random M-i is:5",5<2?or why does it run?
    trace("i is"+i);//out put "i is15"
    N--;
    }
    }

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's the algorithm you're asking about, with line numbers added.

    code:


    1. // Choose N numbers from a list of M numbers (ranging from 0 - M-1)
    2. M = 22;
    3. N = 2;
    4. for (i=0; i<M && N>0; i++) {
    5. if (random(M-i) < N)
    6. {
    7. trace(i); // we pick I
    8. N--;
    9. }
    10. }



    Your questions:

    >> on Line 4, why N>0 ? N =2,2 >0.is it unnecessary?

    Although the loop starts with N equal to 2, everytime it gets a hit, it decrements N, in line 8. When N falls to zero, we have picked two random numbers, and can terminate the loop early.

    >> output "random M-i is:5",5<2?or why does it run?

    The algorithm insures that each time it picks a number, it uses the correct odds for picking it.

    When it attempts to pick the first number, the odds are N/M or 2/22, so 2/22 of the time it will pick the first number. The way this is accomplished is by generating a random number in the range 0 - 21. If it's less than 2, we've met our 2/22 odds.

    If it doesn't pick the first number, there are only 21 numbers remaining and the odds increase to 2/21 or 2/(M-1). Each time it doesn't pick a number, the odds increase this way. If it were to get to the last 2 numbers without picking a number, it will always pick the last two numbers, and indeed the odds of picking the second to last number would be 2/2 or 100%.

    If it DOES pick the first number, we decrement N, which causes the odds of picking additional numbers to decrease. The odds of picking the second number are then 1/21. And so on... If we pick all the numbers, N drops to zero, and the odds of picking any remaining numbers are 0 / something, or zero.
    Last edited by jbum; 08-08-2004 at 04:01 AM.

  8. #8
    Member
    Join Date
    Aug 2004
    Location
    China
    Posts
    43
    thank you for your patient explain.

  9. #9
    World Kit Vote Holder Abelius's Avatar
    Join Date
    Feb 2002
    Location
    US
    Posts
    963
    Beautiful nugget, jbum!
    Cordially,
    Abelius
    www.worldkit.com

  10. #10
    Junior Member
    Join Date
    Sep 2003
    Posts
    18
    Thanks! Just what I was looking for.

    I have a stupid question, though (I am a newbie:-)...How can I use the script in a movie? Right now when I added the script to a button, and I test the movie, the random numbers are displayed in the output window...

    I want the numbers to appear in the movie itself (in a certain font and size). For example, the user has to do something, and in the next scene only the random numbers are displayed.

    How do I do that?

    Thanks in advance!!!
    Do you or your clients need an ecard site/marketing campaign? We have content!
    http://www.cardsup.com/webcontent.php

  11. #11
    setVariable kinx's Avatar
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    239
    Sweet, very handy bit of code mate thanks for that.

    2funzone, you can put it in a function like

    Code:
    function randList(M,N) {
    	ary = [];
    	for (i=0; i<M && N>0; ++i) {
    		if (random(M-i)<N) {
    			ary.push(i+1);
    			N--;
    		}
    	}
    	return(ary);
    }
    and just call it like:

    myText.text = randList(22,6) //when you want 6 numbers returned from a poss set of 22

    Cheers again Jbum

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