2016年6月2日 星期四

亂數<可重複>

<html>
<script>
var answer;

function radom()
{

  answer="";
  var a1 = Math.floor(Math.random()*10);
  var a2 = Math.floor(Math.random()*10);
  var a3 = Math.floor(Math.random()*10);

  answer = a1+""+a2+""+a3;
  document.write(answer);

}
   </script>
<body>
 <input type="button" value="亂數" onclick="radom()"/>
</body>

</html>

亂數<不重複>

<html>
<script>
var answer;
function radom()
{
answer="";
var a1 = Math.floor(Math.random()*10);
var a2 = Math.floor(Math.random()*10);
var a3 = Math.floor(Math.random()*10);
while(a2==a1)
a2 = Math.floor(Math.random()*10);
while((a3==a1)||(a3==a2))
a3 = Math.floor(Math.random()*10);
answer = a1+""+a2+""+a3;
document.write(answer);
}
</script>
<body>
<input type="button" value="亂數" onclick="radom()"/>
</body>
</html>