Skip to content

Instantly share code, notes, and snippets.

@gdibble

gdibble/random-number.js

Last active Aug 29, 2015
Embed
What would you like to do?
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