JavaScript Tip: get a Random Number between two Integers
August 24th, 2010 by Tweet
In JavaScript, we can get a Random number; however, this number is between 0 and 1 which is not very useful when we want to work with integers. For example, executing the random method of the Math Object we can get something like this:
Math.random(); // 0.7427427755721524 Math.random(); // 0.001514993949523702 Math.random(); // 0.4938488937880676
Many languages like PHP has a random function which take two integers as parameters and return a number in the range of the value of the two integers. (The range include the parameters). For example we can do this in PHP:
rand(10,20); //12 rand(10,20); //18 rand(10,20); //17
We can emulate this with the following function:
function randomFromTo(from, to){ return Math.floor(Math.random() * (to - from + 1) + from); } randomFromTo(10,20); //18 randomFromTo(10,20); //10 randomFromTo(10,20); //15 randomFromTo(10,20); //20
What about using it as part of the JavaScript Core?
Most of the developers do not recommend to extend the build-in objects, because it can create confusion between developers working in the same project. However, there are a lot of frameworks that extend the built-in object like Prototype.
The way to do it is using the prototype object that is part of all objects created in JavaScript. Example:
if(!Math.prototype.randomFromTo){ Math.prototype.randomFromTo = function(from, to){ return Math.floor(Math.random() * (to - from + 1) + from); }; }
With this piece of code we can call the method from the Math Object like this:
Math.randomFromTo(100,1000) //950
If you want to go beyond, you can add a new method to the Array Object which would return a random element. Example:
if(!Array.prototype.randomItem){ Array.prototype.ramdomItem = function(){ return this[Math.randomFromTo(0, this.length - 1)]; }; }
Now we can use the randomItem method like this:.
[1,34,56,76,9,67,5].randomItem();
This would return one of the items of the array randomly.
Get on time success in 000-977 certification & 70-291 voice training by using our latest and high quality testking 1Y0-A05 and other superb BH0-006 dumps pass resources. E20-522 dumps are also a key to success.
Teylor Feliz is a well-known XHTML/CSS/JavaScript enthusiast from Dominican Republic. He has more than 10 years experience in the computer programming, graphic design, and web development world, including 2 years teaching computer science. Teylor has a degree in Applied Sciences from the University of Louisiana and is completing a Master’s degree in IT with a concentration in Web Design and Development, at the University of Denver.
Subscribe to our RSS
Please help us to promote this article.






Very informative
Thank you! Glad you liked it and thanks for your comment!
Nice Tip , thanks for sharing
Yup very informative indeed
helping information. thanks
Nice post, thank you very much.
function randomFromTo(from, to){
var temp;
if( from == to ){
return from;
}else if( from > to ){
var temp = from;
from = to;
to = from;
}
return Math.floor(Math.random() * (to – from + 1) + from);
}
function randomFromTo(from, to){
var temp;
if( from == to ){
return from;
}else if( from > to ){
temp = from;
from = to;
to = from;
}
return Math.floor(Math.random() * (to – from + 1) + from);
}
The function that I use still works with negative numbers.
Thank you!
Oh,Sorry, I do not consider the comprehensive. You‘re right~
Thank you for your comment!!
very nice, work really fine thanks for sharing
Nice one! Thanx!