Create Random Number Between Values

How can we create random numbers between two values, e.g: 5 and 15?
Creating a random number value in javascript?


  • A java-script method or function will create random numbers between any two values.
  • This method takes two integer arguments. The second integer should be greater than first integer.
  • Just copy the code in to your page and use it for creating or generating random number between two different numbers.


Random Number Testing :
Enter the numbers between which a random number is to be created.

Test Result :


<!--Script by>
<!-- more scripts @ -->
<script type="text/javascript">
function randomnumber(num1, num2)
num1 = parseInt(num1);
num2 = parseInt(num2);
if(num1 >= num2)
alert("Number 2 should be greater than Number 1");
var generator = Math.random()*(num2-num1);
generator = Math.round(num1+generator);
document.test.result.value = generator;
<!-- Script by -->
  • Release Date - 16-08-2005
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to


  • Here we have explained with a form.
  • Just copy the JavaScript code and use the method or function as random number generator.
  • Creating the form using following HTML code
  •  <form name="test">
       <input name=one type=text size=3>
       <input name=two type=text size=3>
       <input type=button value=test onclick="randomnumber(one.value,two.value)">
       Test Result : <input name=result readonly type=text size=6>
  • The javascript function randomnumber() is triggered on submission of the form.


Related Scripts

Free Javascript Scripts

Ask Questions

Ask Question