服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - JS中产生20位随机数以0-9为例也可以是a-z A-Z

JS中产生20位随机数以0-9为例也可以是a-z A-Z

2021-02-02 16:57whsnow JavaScript

本节主要介绍了JS如何产生随机数,本例产生20位随机数以0-9为例也可以是a-z A-Z,需要的朋友可以参考下

JS代码:

?
1
2
3
4
5
6
7
8
9
10
11
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代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>

延伸 · 阅读

精彩推荐