Skip to content

Instantly share code, notes, and snippets.

@blasten
Created May 8, 2015 01:48
Show Gist options
  • Save blasten/bfe4cda059e2dae692bf to your computer and use it in GitHub Desktop.
Save blasten/bfe4cda059e2dae692bf to your computer and use it in GitHub Desktop.
function randomNumber(min, max) {
min = parseInt(min, 10);
max = parseInt(max, 10);
if (min !== min) {
min = 0;
}
if (max !== max || max <= min) {
max = (min || 1) << 1; //doubling
} else {
max = max + 1;
}
var ratio = Math.random();
var range = max - min;
return ~~(range * ratio + min); // ~~ coerces to an int, but fast.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment