Last active
October 3, 2015 02:28
-
-
Save gengkev/2371754 to your computer and use it in GitHub Desktop.
Random integers in JavaScript...just because it got annoying.
This file contains hidden or 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 randInt(min,max){ | |
var range = max - min; | |
// it actually does work the other way... | |
// if (range < 0) { throw new RangeError("min must be less than max"); } | |
var rand = Math.floor(Math.random() * (range + 1)); | |
return min + rand; | |
} |
This file contains hidden or 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 randInt(a,b){return a+Math.floor(Math.random()*(++b-a))} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment