A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Random Number

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Posts
    127

    Random Number

    I need to create a variable which's number randomaly changes. It must go between 1 and 10, no lower or higher.

    At the moment ive been using:

    onClipEvent(enterFrame){
    variable++
    variable++
    variable++
    etc...
    if(variable==10){
    variable=0
    }


    This works fine, but it goes 1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9, etc. I need it to go anything like: 2,4,3,2,2,3,4,6,7,8,5,6,7,8,8,5,4,3,5,6,10,8. The numbers must just come up randomally. Possible? Please?

  2. #2
    Member
    Join Date
    Oct 2003
    Location
    Finland
    Posts
    84
    code:

    onClipEvent(enterFrame){
    variable=Math.round(Math.random()*10)
    }



    or:

    code:

    onClipEvent(enterFrame){
    variable=random(10)+1
    }



    The first one is recommended though.

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Posts
    127
    Stunning mate!! Thanks very much, works exactly how i want it

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Math.random() returns a value which is >= zero and < one. It might be zero, but it will never ever be one (although it might be .9999999).

    Math.round() will round in either direction. Math.floor() always rounds down, and Math.ceil() always rounds up.

    Because of this, Math.round(Math.random()*10) will occasionally give you a zero. It also has an uneven distribution like so:

    0 5%
    1 10%
    2 10%
    3 10%
    4 10%
    5 10%
    6 10%
    7 10%
    8 10%
    9 10%
    10 5%

    Better to use either of the following, which deliver 1-10 with even distribution:

    a) Math.floor(Math.random()*10)+1
    b) random(10)+1

    a and b are exactly the same, but b is more readable. b is "deprecated" however I use it all the time, and I seriously doubt it will ever go away - it's just too darn convenient.

    A lot of folks on this board will tell you to use this one:

    c) Math.ceil(Math.random()*10)

    It seems simpler because it doesn't have a "+1" and if you test it, it will appear to have identical results to methods a and b.

    HOWEVER, it actually has a VERY slight chance (something like 1 in a billion) of producing zero as well (because Math.random() may return zero, and Math.ceil(0) is zero.

    Finally, if you are using random numbers and arrays a lot, you will find it easier to name your movieclips starting from 0, rather than starting from 1. This way you can just use this:

    d) random(10)

    and it will refer to objects 0 thru 9 with even distribution.


    - Jim

  5. #5
    Junior Member
    Join Date
    Jun 2006
    Posts
    6

    yeh

    That's all good and well but I also want a code that will go something along the lines of

    If variable = 1 {gotoAndPlay(3)}
    If variable = 2 {gotoAndPlay(5)}

    Any help?

  6. #6
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    The frame number is always double the variable value + 1? If so you might use,

    Code:
    myVar = 1;
    gotoAndPlay((2 * myVar) + 1); // goto frame 3
    if you wanted to test for different values and then send the movie to a different frames you could do something like this,

    Code:
    if (myVar == 1) {
        gotoAndPlay(3);
    } else if (myVar == 2) {
        gotoAndPlay(5);
    } else {
        gotoAndPlay("wherever");
    }

  7. #7
    Junior Member
    Join Date
    Jun 2006
    Posts
    6

    ...

    ... Ca you put in the whole code and explain every part of it because I am only 14 and I don't know what your talkin about and I don't see a random number generator code in what you just gave me.

  8. #8
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    to get the random number you would use one of the methods posted by jbum,

    for example,

    var myVar = Math.floor(Math.random()*10) + 1; // myVar will be a random number between 1 and 10

    then you could use this value to send the movie to a different frame,

    gotoAndPlay((2 * myVar) + 1); // if myVar was 2 this would send the movie to frame 5

  9. #9
    Junior Member
    Join Date
    Jun 2006
    Posts
    6
    Close but not what I'm looking for. I want to have it gotoAndPlay if myVar equals a certain number.

  10. #10
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Quote Originally Posted by catbert303

    if you wanted to test for different values and then send the movie to a different frames you could do something like this,

    Code:
    if (myVar == 1) {
        gotoAndPlay(3);
    } else if (myVar == 2) {
        gotoAndPlay(5);
    } else {
        gotoAndPlay("wherever"); // or don't do anything if it should only goto a frame for certain values
    }

  11. #11
    Junior Member
    Join Date
    Jun 2006
    Posts
    6
    It won't work. It says that and Identify is expected.

  12. #12
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Try the attached file
    Attached Files Attached Files

  13. #13
    Junior Member
    Join Date
    Jun 2006
    Posts
    6
    I'm sorry but I couldn't understand that...

  14. #14
    Junior Member
    Join Date
    Jun 2006
    Posts
    6
    Could you simplify it?

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