A Flash Developer Resource Site

1. ## 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. You should take a random number between 0-20, then add 40.
Pseudo code/AS3 :
enemyHP = 40+Math.random()*20;

3. 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. 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. enemyHP = 40 + random(20);
that should give you round number.
but if that doesn't work,
enemyHP = Math.round(40 + random(20));

6. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap