A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Random numbers

Hybrid View

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    7

    Smile Random numbers

    OK, so i've got that

    Code:
    enemyHP = random(60);
    Makes the enemyHP a number from 0 to 60 in integers, but how do i make it so it goes from 40 to 60 instead, i need this for a game because an enemy starting with 1 HP is way too easy, but i want randomness. Please help, even if you don't know, just tell me what you THINK might do it.

  2. #2
    Member
    Join Date
    Feb 2009
    Location
    Paris
    Posts
    79
    You should take a random number between 0-20, then add 40.
    Pseudo code/AS3 :
    enemyHP = 40+Math.random()*20;

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    7
    ooops, whould have been more clear, i'm doing this in AS2, not that it matters, even i should be able to translate that... thanks.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    7
    Oh, that worked perfectly as it is in AS2, but i need a way to make it an integer... (total noob when it comes to this) I'd love help but i'll probably be able to work it out my self...

    Thanks for the help diuck.

  5. #5
    |-'|-'|
    Join Date
    Jan 2006
    Posts
    273
    enemyHP = 40 + random(20);
    that should give you round number.
    but if that doesn't work,
    enemyHP = Math.round(40 + random(20));

  6. #6
    Musical chair sumwungye's Avatar
    Join Date
    Jan 2008
    Location
    Planet Earth, third planet from the Sun
    Posts
    287
    Hello

    Sort of adding onto the other responses...

    Math.random() gives you a random number between 0 and 1.

    random(x) gives you a random integer from a list with length x starting at zero (in other words, a random integer from 0 to one less than x).

    Because Math.random() always gives you an irrational number, Math.random()*x gives you an irrational number also...just with a different range.

    So, if you want to make it rational again, just do Math.floor(Math.random()*x) to take away all decimal places.

    So, random(x) is the same thing as Math.floor(Math.random()*x) except random(x) is now deprecated, as I've been told.

    Just clarifying things a little
    --SumWunGye

Tags for this Thread

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