急!急! js中产生不一样的随机数!
如1-30之间,我想产生10个随机整数,但这10个(或15个)个数必须不一样,求高手解答~
[解决办法]
<script type="text/javascript">
var r=parseInt(Math.random()*30);
var array=[];
for(var i=0;i<11;i++)
{
var flag=0;
do
{
for(var j=0;j<array.length;j++)
{
if(array[j]==r) {flag=1;break;}
}
if(!flag)
{
array[array.length]=r;
}
else
{
r=parseInt(Math.random()*30);
}
}while(!flag);
}
for(var j=0;j<array.length;j++)
document.write(array[j]+" ");
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function getNumb(){
var num = new Array();
for(var i = 0; i < 10; i++){
var val = Math.ceil(Math.random() * 30);
var isEqu = false;
for(var idx in num){
if(num[idx] == val){isEqu = true; break;}
}
if(isEqu)
i--;
else
num[num.length] = val;
}
return num;
}
function test(){
alert(getNumb());
}
//-->
</SCRIPT>
</HEAD>
<BODY onload="test()">
</BODY>
</HTML>