Skip to content

Instantly share code, notes, and snippets.

@gengkev
Last active October 3, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gengkev/2371754 to your computer and use it in GitHub Desktop.
Save gengkev/2371754 to your computer and use it in GitHub Desktop.
Random integers in JavaScript...just because it got annoying.
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;
}
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