Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Created July 23, 2014 21:12
Show Gist options
  • Save pinalbhatt/151d0b62501d3413647e to your computer and use it in GitHub Desktop.
Save pinalbhatt/151d0b62501d3413647e to your computer and use it in GitHub Desktop.
Javascript: Random Number between two integers
/*
Javascript: Random Number between two integers
http://blogs.pbdesk.com/javascript-random-number-between-two-integers/
*/
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
//Invocation:
var rnd = randomFromTo(5,20);
/*
And if want to extended your Math Library with this new functionality here is your prototype extension code:
*/
if(!Math.prototype.randomFromTo){
Math.prototype.randomFromTo = function(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
};
}
//Invocation:
Math.randomFromTo(5,25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment