Sunday, January 17, 2010

How to generate a random number in javascript


How to generate Random Numbers in JavaScript?




Following example shows you how to generate Random Numbers in JavaScript.





In some situations you may want to generate random numbers in your javascripts. The Math.random() method returns a floating-point number between 0 and 1. In this example shows how to create a simple game using javascript random numbers.




Demo- Generate Random Numbers using JavaScript.



JavaScript Random Number Generator
      
1. How to generate Random number between 1-10 ?   
2. How to generate Random number between 1-100 ?   
3. How to generate Random number up to entered integer value?    
Enter value  
  
 
Simple game using JavaScript Random Numbers
Guess a number and color. Then press Try button to check it.
       
 
Pick a number
 
Pick a color
   
     








1. Example code for generate random numbers using JavaScript.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function getRandomNumber(randomUpTo){

var myRandomNumber=Math.floor(Math.random() * (randomUpTo+1));

alert(myRandomNumber);

}



function validateRandomNumberLimit(){

var txtField=document.getElementById("txtRandomNumberLimit");

var limit=txtField.value;



if(isNaN(limit)==false && limit>0){



}else{

txtField.value=5;

txtField.select();

}

}



function generateRandom(){

var limit=document.getElementById("txtRandomNumberLimit").value;//alert(limit);

getRandomNumber(parseInt(limit));

}



function checkSelection(){

var color=parseInt(document.getElementById("RadioGroupColors").value);

var number=parseInt(document.getElementById("RadioGroupNumbers").value);



var randomNumber=Math.floor(Math.random() * (5));

var randomColor=Math.floor(Math.random() * (4));



var colors=Array("Red","Green","Pink","Yellow","Black");

if(color==randomColor && number==randomNumber){

alert("Congratulations!\nYou are the winner");

}else{

alert("Sorry!\nPlease try again\nComuter selection was Number="+number+" Color="+colors[randomColor]);

}

}

-->

</script>

Html Code



<html>

 <head>

  <title>How to generate random numbers using JavaScript </title>

 </head>

<body>

<table border="0">

<tr>

<td align="left">1. How to generate Random number between 1-10 ? </td>

<td align="left">&nbsp;&nbsp;</td>

<td align="left"><label>

<input name="btnRandomNumber1" type="button" id="btnRandomNumber1" value="Get Random" onclick="javascript:getRandomNumber(11);" />

</label></td>

</tr>

<tr>

<td align="left" valign="top">2. How to generate Random number between 1-100 ? </td>

<td align="left">&nbsp;&nbsp;</td>

<td align="left"><input name="btnRandomNumber2" type="button" id="btnRandomNumber2" value="Get Random" onclick="javascript:getRandomNumber(100);" /></td>

</tr>

<tr>

<td align="left" valign="top">3. How to generate Random number up to entered integer value? </td>

<td align="left">&nbsp;</td>

<td align="left">&nbsp;</td>

</tr>

<tr>

<td align="left" valign="top"><table width="100%" border="0">

<tr>

<td>Enter value </td>

<td><label>

<input name="txtRandomNumberLimit" type="text" id="txtRandomNumberLimit" value="5" size="5" onkeyup="javascript:validateRandomNumberLimit();" />

</label></td>

<td>&nbsp;</td>

</tr>

</table></td>

<td align="left">&nbsp;&nbsp;</td>

<td align="left"><label>

<input name="btnRandomNumber3" type="button" id="btnRandomNumber3" value="Get Random" onclick="javascript:generateRandom();" />

</label></td>

</tr>

<tr>

<td colspan="3" align="left" valign="top">&nbsp; </td>

</tr>

</table>

</body>

</html>

 




 »   How to get screen resolution in JavaScript
 »   How to limit characters in textarea using JavaScript
 »   How to validate decimal number in JavaScript
 »   How to validate an email address in JavaScript
 »   How to validate date using JavaScript
 »   JavaScript String functions
 »   How to validate multiple select list box in JavaScript
 »   How to generate random numbers in JavaScript
 »   How to validate multiple check box in JavaScript
 »   How to validate user login in JavaScript
 »   How to validate drop down list in JavaScript
 »   How to validate radio button group in JavaScript
 »   How to create JavaScript alerts
 »    How to create popup windows in JavaScript
 »   How to count words in a text area using JavaScript

©-Copyright By Duminda Chamara  
JavaScript Validation  

0 comments: