DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS中產生20位隨機數以0-9為例也可以是a-z A-Z
JS中產生20位隨機數以0-9為例也可以是a-z A-Z
編輯:關於JavaScript     

JS代碼:

function s20(){ 
var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; 
for(var j=0;j<500;j++){ //500為想要產生的行數
var result="";
for(var i=0;i<20;i++){ //產生20位就使i<20
r=Math.floor(Math.random()*16); //16為數組裡面數據的數量,目的是以此當下標取數組data裡的值! 
result+=data[r]; //輸出20次隨機數的同時,讓rrr加20次,就是20位的隨機字符串了。 
} 
document.write(result); 
document.write("<br/>"); 
} }

完整html代碼:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<script type="text/javascript"> 
function s20(){ 
var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; 
for(var j=0;j<500;j++){ 
var result=""; 
for(var i=0;i<20;i++){ 
r=Math.floor(Math.random()*16);

result+=data[r]; 
} 
document.write(result); 
document.write("<br/>"); 
} } 
</script> 
</head> 
<body> 
<input type="button" onclick="s20()" value="產生隨機數"> 
</body> 
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved