USERNAME:
PASSWORD:
Save Info!
Logging in…
This topic is 1 page long.
[ Profile | Posts | Contact ]
Posted at: 8/1/08 06:54 AM
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?
Posted at: 8/1/08 07:01 AM
Sign-Up: 09/17/04
Posts: 1,517
Math.round(Math.random()*5)
Sig by ApothicFlash <3
Posted at: 8/1/08 07:06 AM
At 8/1/08 07:01 AM, CaiWengi wrote: Math.round(Math.random()*5)
Great Thx.
Posted at: 8/1/08 07:13 AM
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
Posted at: 8/1/08 07:26 AM
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 ||========
Posted at: 8/1/08 08:43 AM
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 :)
Posted at: 8/1/08 08:44 AM
Yikes sorry re-read. You're completely right. My test was inconclusive. Be a dear and rewrite the function to work evenly :)
All times are Eastern Standard Time (GMT -5) | Current Time: 02:02 AM
<< Back