2016年6月3日 星期五

不重複隨機亂數

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>第一支javaScript</title>
</head>
<body>
<h2>document.write用法</h2>
 <Script type ="text/javascript">

var n=0,p=0,q=0;
var maxNum =9;
var minNum = 0;


n=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
p=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
q=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;

for(var i=0; i<5; i++) {

if(n==p)
{
p=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
}
if(p==q)
{
q=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
}
if(q==n)
{
n=Math.floor( Math.random() * (maxNum - minNum + 1) ) + minNum;
}
}
  document.write("亂數",n,p,q);
 </Script>
</body>
</html>

沒有留言:

張貼留言