Created
December 15, 2010 15:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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