Skip to content

Instantly share code, notes, and snippets.

@kaw2k
Created December 5, 2012 01:00
Show Gist options
  • Save kaw2k/4210929 to your computer and use it in GitHub Desktop.
Save kaw2k/4210929 to your computer and use it in GitHub Desktop.
A simple extension of the javascript Number class for ease of generating random numbers.
// Example use
// var arr = [1, 2, 3, 4, 5];
// var len = arr.length;
// var item = arr[len.random()];
Number.prototype.random = function(inc) {
var val = (inc) ? 1 : 0;
return Math.floor(Math.random() * (this.valueOf() + val));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment