## AS2 random() problem

• 12,062 Views
• 6 Replies
rayofhopemedia
rayofhopemedia
• Member since: Apr. 27, 2006
• Offline.
Forum Stats
Member
Level 18
Blank Slate
AS2 random() problem 2007-09-16 14:43:16

hey, so i want my variable to be a random number between 10 and 20, how would you do this? i found a site with an examplae that said it would be x = Math.random(10)*(20); but that means a random number between 0 and 10 multiplyed by 20 dosent it?
sorry if this is a nooby question but i realy cant find the answer.

Kart-Man
Kart-Man
• Member since: Jan. 7, 2007
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS2 random() problem 2007-09-16 14:46:14

someVar = random(20)+10;

This will make the variable a number between 10 and 20.

postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN

Pyromaniac
Pyromaniac
• Member since: Jan. 14, 2005
• Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS2 random() problem 2007-09-16 14:50:23

At 9/16/07 02:46 PM, Kart-Man wrote: someVar = random(20)+10;

This will make the variable a number between 10 and 20.

Thats acually between 20-30, between 10 and 20 would be
random(10)+10

Kart-Man
Kart-Man
• Member since: Jan. 7, 2007
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS2 random() problem 2007-09-16 14:54:27

At 9/16/07 02:50 PM, Pyromaniac wrote:
At 9/16/07 02:46 PM, Kart-Man wrote: someVar = random(20)+10;

This will make the variable a number between 10 and 20.
Thats acually between 20-30, between 10 and 20 would be
random(10)+10

Ah, you're right. Whoops. :P
*blames keyboard for mistake*

postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN

gankro
gankro
• Member since: Sep. 16, 2006
• Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS2 random() problem 2007-09-16 14:59:19

if you want it to be a whole integer then wrap it in Math.random()

My Coding Portfolio (updated frequently)
If you stay in bed for long enough it will be socially acceptable to go to bed again

GrimNicker
GrimNicker
• Member since: Aug. 7, 2007
• Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to AS2 random() problem 2007-09-16 15:53:36

At 9/16/07 02:50 PM, Pyromaniac wrote:
At 9/16/07 02:46 PM, Kart-Man wrote: someVar = random(20)+10;

This will make the variable a number between 10 and 20.
Thats acually between 20-30, between 10 and 20 would be
random(10)+10

random(10)+10 is between 0 and 19.
it should be random(10)+11

GrimNicker
GrimNicker
• Member since: Aug. 7, 2007
• Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to AS2 random() problem 2007-09-16 15:55:20

sorry (9 and 19) I meant