Home > Web Front-end > JS Tutorial > Generate 20-digit random numbers in JS, taking 0-9 as an example, it can also be a-z A-Z_javascript skills

Generate 20-digit random numbers in JS, taking 0-9 as an example, it can also be a-z A-Z_javascript skills

WBOY
Release: 2016-05-16 16:40:40
Original
1781 people have browsed it

JS code:

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/>"); 
} }
Copy after login

Full html code:

<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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template