dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: google has faild me on a simple question.

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Posts
    170

    google has faild me on a simple question.

    If I wanted the range of the random numbers for flash to be between 3 to 8, I would rewrite the below actionscript to..?

    (random(3)+1)

  2. #2
    absent
    Join Date
    Aug 2000
    Location
    NB, Canada
    Posts
    155
    I'd use: random(6)+3;

    g'luck.
    Last edited by gomar; 10-27-2005 at 08:11 AM. Reason: typo
    - g

  3. #3
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    The use of random() has been deprecated. You should use Math.random() instead.
    Code:
     var someNum:Number = Math.floor( Math.random() * 6 )+3;
    trace( someNum );
    Per the documentation on LiveDocs, Math.random() will return a number between [0 and 1), or as they write it, 0 <= n < 1. Which means you can potentially return a zero, but never a 1, and anything in-between. So in the previous code, the maximum value you can get will be 5.999999 and never six. Math.floor() will drop the decimal portion of it, returning a number between 0 and 5, add 3, you get between 3 and 8.

    Of course, if you're looking for decimals as well, get rid of the Math.floor() function
    Last edited by wombatLove; 10-27-2005 at 08:26 AM. Reason: Spell check

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