Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created December 15, 2010 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieMason/742054 to your computer and use it in GitHub Desktop.
Save JamieMason/742054 to your computer and use it in GitHub Desktop.
Returns a random number between a specified range, optionally set to a fixed number of decimal places
/**
* Returns a random number between a specified range, optionally set to a fixed number of decimal places
*
* @author https://github.com/JamieMason
* @param {Number} minimumValue The lowest value the random number can be
* @param {Number} maximumValue The highest value the random number can be
* @param {Number} decimalPlaces Optional value to set the number of decimal places
* @type Number
*/
function getRandomNumber(minimumValue, maximumValue, decimalPlaces)
{
var randomValue = minimumValue + (Math.random() * (maximumValue - minimumValue));
return typeof decimalPlaces === 'undefined' ?
Math.round(randomValue)
:
randomValue.toFixed(decimalPlaces);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment