Created
May 8, 2015 01:48
-
-
Save blasten/bfe4cda059e2dae692bf to your computer and use it in GitHub Desktop.
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
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