How to generate Random Numbers in JavaScript?
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? | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
Simple game using JavaScript Random Numbers | |||||||||||||||||||||||||||||||||||
|
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"> </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"> </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"> </td>
<td align="left"> </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> </td>
</tr>
</table></td>
<td align="left"> </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"> </td>
</tr>
</table>
</body>
</html>
JavaScript Validation
0 comments:
Post a Comment