js产生随机数

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js产生随机数</title>
</head>
<body>
<form name="lotto">
<table width="100%" border="0">
<tr>
<td align="center">
产生数字个数
<select name="numbercount">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6" selected="selected">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br />
产生随机数范围<input type="text" name="maxnum" value="49" size="2" maxlength="2"/>
<br />
<input type="button" value="产生随机数" onclick="numbers()"/>
<textarea name="results" rows="11" cols="15"></textarea>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function numbers(){
var nummenu = document.lotto.numbercount;
var numbercount = nummenu.options[nummenu.selectedIndex].value*1;
var maxnumbers = document.lotto.maxnum.value*1;
if(numbercount > maxnumbers){
alert("Be sure to select a max lottery number value!");
}
else{
var ok = 1;
r = new Array (numbercount);
for (var i=1; i<= numbercount; i++){
r[i]=Math.round(Math.random() * (maxnumbers-1))+1;
}
for(var i=numbercount; i >= 1; i--){
for(var j=numbercount;j >= 1; j--){
if((i!=j)&&(r[i] ==r[j])) ok = 0;
}
}
if(ok){
var output = "";
for(var k =1; k<= numbercount; k++) {
output +="Number " + k + " = " + r[k] + "\n";
}
document.lotto.results.value = output;
}
else numbers();
}
}
</script>
</body>
</html>

你可能感兴趣的:(js产生随机数)