Created
July 23, 2014 21:12
-
-
Save pinalbhatt/151d0b62501d3413647e to your computer and use it in GitHub Desktop.
Javascript: Random Number between two integers
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
/* | |
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