1 /**
  2  * @fileOverview This file contains new methods for the Math namespace
  3  */
  4 /**
  5  * @name Math
  6  * @namespace A built in namespace containing mathmatical constants and functions.
  7  */
  8 
  9 /**
 10  * Extension to Math.random which returns an integer between 2 values instead of
 11  * a random float.
 12  * Partly borrowed from {@link https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Math/random#Example.3a_Using_Math.random}
 13  * 
 14  * @param {Number} min The lowest number (inclusive) to return
 15  * @param {Number} max The highest number (inclusive) to return
 16  * @return {Number} A random integer between min and max
 17  */
 18 Math.rand = function rand(min, max) {
 19 	return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
 20 };
 21 
 22 /**
 23  * Calculate the sum of a list of numbers. Is also useful as an argument to .reduceNative
 24  * 
 25  * @param {Number} ... Arguments forming a list of numbers to sum up tp a total
 26  * @return {Number} The total sum of all the numbers passed
 27  */
 28 if(!Math.sum)
 29 	Math.sum = function sum(/*...*/) {
 30 		if( !arguments.length )
 31 			throw new TypeError();
 32 		var args = Array.slice(arguments);
 33 		var s = args.shift();
 34 		while( args.length )
 35 			s += args.shift();
 36 		return s;
 37 	};
 38 
 39 /**
 40  * Calculate the average value from a list of numbers.
 41  * 
 42  * @param {Number} ... Arguments forming a list of numbers to create and average of
 43  * @return {Number} The average of all the numbers passed
 44  */
 45 Math.avg = function avg(/*...*/) {
 46 	return Math.sum.apply(Math, arguments) / arguments.length;
 47 };
 48 
 49