<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js生成随机数</title>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
</head>
<body>
<script>
function _getRandomString(length) {
length = length || 32;
//设置随机数范围
var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var maxPos = $chars.length;
var result = '';
for(i = 0; i < length; i++) {
//产生随机数方式
result += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return result;
}
for(var j = 0; j < 5; j++) {
//随机数长度为4
var randomNumber = _getRandomString(4);
console.log(randomNumber);
}
</script>
</body>
</html>
js生成随机字符串
最新推荐文章于 2024-08-30 13:48:36 发布