A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: random command

  1. #1
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870

    random command

    Hello..

    Can any clever nuts figure out why this actionscript is not working?

    Code:
    //numRange, lowest are all variables of input txtboxes 3 frames before this AS frame
    var rad1 = random(numRange)+lowest;
    // rad1 is another dynamic textbox set on THIS frame
    // and on the next frame it is previousFrame();
    This is my first try at loading random command using a variable and several imputs.

    Thanks Guys...
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    What exactly it's not working for you?

    For example:
    code:

    var numRange = 5;
    var lowest = 15;
    var rad1 = random(numRange) + lowest;


    will give you a number between 15 and 19, i.e., 15, 16, 17, 18, 19 - 5 options, where the lowest is 15.

    Just one detail: random() is deprecated and Math.random() should be used instead.
    Here's the equivalent code:
    code:

    var rad1 = Math.floor(Math.random() * numRange) + lowest;


  3. #3
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870
    var numRange = 5;
    var lowest = 15;
    var rad1 = random(numRange) + lowest;

    yea something like that but numRange and lowest are user defined, this means the user gets to type it in, press a button to enter the selection frame - then a countdown starts, and when the countdown reaches 0 the computer sends the final random number to frame 10 which stops the random generator.
    So I made it to be user Defined but ..
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

  4. #4
    Senior Member the_one_18's Avatar
    Join Date
    Mar 2002
    Posts
    217
    first of all i think you cant put the number just like that
    rad1.text = toString(Math.random(numRange) + lowest);
    try sending us the fla to see what you really mean
    MeHdI

  5. #5
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870
    OK maybe I will

    Here we go.

    p.s.Thanks for the replies everyone but the problem's not fixed
    Attached Files Attached Files
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

  6. #6
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    I haven't taken a look at your fla yet, but I do find it helpful to use a function like this:

    Code:
    public static function generateRandomNumber(min:Number, max:Number):Number {
    	return(Math.abs(random((max - min) + 1)) + (min));
    }
    In a class. Or if you're not using AS 2.0, just remove the public static part.

    Anyways, you can call that function any time you want a number within a constrained range and it'll return a random number within that range.

    I suppose I just find that a little more useful. Hope it helps you.

  7. #7
    Senior Member the_one_18's Avatar
    Join Date
    Mar 2002
    Posts
    217

    Ok

    OK mate, i only changed the random to Math.random
    thats how you use random func in flash
    and lowered the frame rate so u see whats happening
    I hope its the way you want
    Attached Files Attached Files
    MeHdI

  8. #8
    XRave tongxn's Avatar
    Join Date
    Apr 2005
    Location
    Somewhere near Here
    Posts
    870
    LOL i've taken a look

    Nope don't work it started to give this as the random number:

    0.920742629486394
    0.270472850274952
    0.246791299898173

    and so and so on... i can't get the decimal placemark off.

    Thank you for your time but also the time limit is manufunctioning.

    I wonder what's wrong with this script:

    Code:
    if (timeAmount == timeLeft) {
    	gotoAndStop(10);
    }
    LOL it just don't work!
    When you actually know what "OMG I have so much homework!" means, you won't want to be me.
    Xrave

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