Forum Topic: as Q, math.random()?

(182 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

Dqws

Reply To Post Reply & Quote

Posted at: 8/1/08 06:54 AM

Dqws LIGHT LEVEL 16

Sign-Up: 05/17/07

Posts: 426

i have understood how math.random() works,but one Question remain still,
How can i math.random() an integer?
i mean so it will be not a 2.242134 but only 2.0 or 3.0?

BBS Signature

None

CaiWengi

Reply To Post Reply & Quote

Posted at: 8/1/08 07:01 AM

CaiWengi EVIL LEVEL 07

Sign-Up: 09/17/04

Posts: 1,517

Math.round(Math.random()*5)

Sig by ApothicFlash <3

BBS Signature

None

Dqws

Reply To Post Reply & Quote

Posted at: 8/1/08 07:06 AM

Dqws LIGHT LEVEL 16

Sign-Up: 05/17/07

Posts: 426

At 8/1/08 07:01 AM, CaiWengi wrote: Math.round(Math.random()*5)

Great Thx.

BBS Signature

None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/1/08 07:13 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

Math.random() will never return more than 1.

You can make either 2 or 3 by doing:

trace(2+(Math.round(Math.random())));

Or you could make it do any range with this function:

function randomInt(min:Number, max:Number):Number {
	return min+Math.round(Math.random()*(max-min));
}
trace(randomInt(4, 8));

This returns 4, 5, 6, 7 or 8

- Matt, Rustyarcade.com


None

Vengeance

Reply To Post Reply & Quote

Posted at: 8/1/08 07:26 AM

Vengeance EVIL LEVEL 28

Sign-Up: 03/18/05

Posts: 5,033

It would be a stupid idea to use Math.round() with it, simply because the last 2 digits have half the chance of appearing as the ones in the middle.
For example:
Math.round(Math.random()*5);
Will return 0 when between 0 and 0.5;
Will return 1 when between 0.5 and 1.5;
Will return 2 when between 1.5 and 2.5;
Will return 3 when between 2.5 and 3.5;
Will return 4 when between 3.5 and 4.5;
Will return 5 when between 4.5 and 5;

So what you should be doing is:
Math.floor(Math.random()*6)+1 = between 0-5
or
Math.ceil(Math.random()*6)-1 = between 0-5

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/1/08 08:43 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

I ran mine 100 times and they came up equal. I didn't work it out at all but I'm pretty sure it works fairly.

Was that aimed at me or the other fellow?

Cheers :)

- Matt, Rustyarcade.com


None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/1/08 08:44 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

Yikes sorry re-read. You're completely right. My test was inconclusive. Be a dear and rewrite the function to work evenly :)

- Matt, Rustyarcade.com


All times are Eastern Standard Time (GMT -5) | Current Time: 02:02 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!