|
Math.abs(someNumber);
Computes an absolute value. -123 become 123. Handy if you're not sure which number is going to be larger when you subtracting one from another.
Math.floor(someNumber);
Converts a floating point number (1.234) to an integer (1,2,3) by rounding its decimal values down to zero. Similar to Math.ceil(someNumber) and Math.round(someNumber)
myNumber = 1.5;
trace(Math.floor(myNumber));
Yields:
1
In other words, it strips off any decimal places.
Math.max(firstNumber, secondNumber);
Returns the larger of the two numbers. Useful for Math.random() method.
Math.min(firstNumber, secondNumber);
Returns the smaller of the two numbers. Useful for Math.random() method.
Math.random();
Returns a pseudo-random many digit number between 0.0 and 1.0.
For example: 0.365308396052569 (number of digits vary by OS and CPU)
So , to get a usable integer you multiply the result by 10, 100, 1000 etc. to get numbers from 0 to 9, 0 to 99, 0 to 999 etc.
trace( Math.random()*100);
Then strip off the excess decimal places with .floor()
trace(Math.floor( (Math.random()*100));
returns a random number between 0 and 99
But what about a range of numbers? Like 3 to 54.
minVal = 3;
maxVal = 54;
//get a random number
ranNum = Math.random();
trace("ranNum is: " + ranNum);
//multiply the random number by distance
//between the maximum and minimum values,
//then chop off the decimals
wholeNum = Math.floor(ranNum*(maxVal + 1 - minVal));
trace("wholeNum is: " + wholeNum);
//Boost the number up by the minimum
rangeNum = minVal + wholeNum;
trace("rangeNum is: " + rangeNum);
|