View Full Version : [AS2] random things
ccoonnyy
02-23-2009, 09:05 PM
can someone help me with some random functions? lets say you have item 1 wich is a gun and thats make you 20% more likely to succed , how do you write that in actionscript?
and how do you do that when you have the gun, to make it randomly work or not
m4r7in
02-24-2009, 08:19 AM
probability = 80;
if(random(100) > probability)
{
shoot!();
}
Ciubhran
02-24-2009, 02:08 PM
think it's actually:
1 + Math.floor(Math.random() * 100); gives you an integer value between 1 and 100.
fenstalker
02-24-2009, 04:31 PM
think it's actually:
1 + Math.floor(Math.random() * 100); gives you an integer value between 1 and 100.
On rare occasion the above approach will yield a value of 101, so if that is undesirable you'll want to use:
Math.floor(Math.random() * 99.99) + 1
This will give you a balanced range of randomness from 1 to 100.
m4r7in
02-24-2009, 04:37 PM
think it's actually:
1 + Math.floor(Math.random() * 100); gives you an integer value between 1 and 100.
random(100)+1 works
bluemagica
02-24-2009, 04:39 PM
umm.... why not just use Math.ceil(Math.random()*100)
m4r7in
02-24-2009, 06:23 PM
umm.... why not just use Math.ceil(Math.random()*100)
random(100)+1 is shorter :p
Random is depreciated, so you should be using Math.random now, but regardless of that, there are differences between these methods of rounding guys...
Math.floor() = The closest integer that is less than or equal to the specified number
Math.ceil() = The closest integer that is greater than or equal to the number.
Math.floor(0.9) = 0
Math.ceil(0.9) = 1
Also, Math.random gives you any floating point number/integer between 0, and the number given, NOT between 1 and the number as stated above.
Or simply, Math.random()*1 would range from 0 to 0.9999999999; not actually 1.
The reason people append the "+1" into their random scripts is to change that so it goes from 1 to the number given instead.
Here is how I do it myself:
If I have a range, I use this function:
function rN(lowValue:Number, highValue:Number):Number {
return ((Math.random()*(lowValue-highValue-1)) >> 0)+highValue;
}
//Returns random between 5 and 10:
var ranNum:Number = rN(5, 10);
If I just want a straight random that starts at 0 and includes the number given, I do this:
//Returns random between 0 and 10:
var ranNum:Number = (Math.random()*11 >> 0);
If anyone's curious, ">> 0" is a bitwise shift (right), which pretty much does the same thing as floor, except it is much, much faster to execute.
m4r7in
02-25-2009, 12:01 AM
function rN(lowValue:Number, highValue:Number):Number {
return ((Math.random()*(lowValue-highValue-1)) >> 0)+highValue;
}
//Returns random between 5 and 10:
var ranNum:Number = rN(5, 10);
hmm, i think it should be
return ((Math.random()*(highValue-lowValue-1)) >> 0)+lowValue;
?
Heh, it doesn't make any difference, but almost every time I post that script someone mentions it...
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.