Skip to content

Instantly share code, notes, and snippets.

@gdibble
Last active August 29, 2015 14:19
Show Gist options
  • Save gdibble/ed159bdf1a31b90180ec to your computer and use it in GitHub Desktop.
Save gdibble/ed159bdf1a31b90180ec to your computer and use it in GitHub Desktop.
random number generator - lower/upper range, as integer or not
/*
* return random number within lower/upper range
* output a whole number by passing returnInteger=1
* Examples: $R(0, 10); >>> 3.141592653589793
* $R(0, 10, 1); >>> 7
*/
function $R(lowerLimit, upperLimit, returnInteger) {
var step1 = Math.random() * (upperLimit - lowerLimit);
if (returnInteger && (returnInteger === 1)) {
return lowerLimit + parseInt(step1, 10);
} else {
return lowerLimit + step1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment