x 是你想要的条数
<script language="JavaScript">
<!-- Hide
var rand1 = Math.floor(Math.random()*x+1))
quotes = new Array
quotes[1] = '123123'
quotes[2] = '12322222'
quotes[3] = '655555555555'
quotes[4] = '666666666'
quotes[5] = '7777777'
quotes[6] = '8888'
quotes[7] = '999'
quotes[8] = '0000'
quotes[9] = '222'
quotes[0] = '111'
....
quotes[x] = '11145345'
var quote = quotes[rand1]
//-->
</script>
应该可以,没尝试~~~
拼接好数字字母的字符串,然后利用js的随机函数生成拼接好的字符串中的索引值,即可获得一个随机字符,循环12次(取12位)即可得到想要的随机字符串。
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {text-align:center}
input {height:34px padding:0 8px line-height:34px font-size:14px font-family:consolas}
#range {width:60px}
</style>
</head>
<body>
<p>
生成位数:
<input type="text" id="range" placeholder="" value="12" />
<input type="button" value="生成" onclick="getRandomStr()" />
</p>
<p>
结果:<input type="text" id="result" />
</p>
<script>
function getRandomStr() {
var nums = '0123456789',
lowerCase = 'abcdefghijklmnopqrstuvwxyz',
chars = nums + lowerCase + lowerCase.toUpperCase()
var len = document.getElementById('range').value,
charsLen = chars.length
result = ''
if (isNaN(len)) {
alert('生成位数必须是数字!')
return
}
for (i = 0 i < len i++) {
result += chars.charAt(Math.floor(Math.random() * charsLen))
}
document.getElementById('result').value = result
}
</script>
</body>
</html>
参考代码:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>自动生成12个大写字符串</title>
</head>
<body>
<h1>12个大写字母</h1>
<div>
</div>
<button onclick="random_A()">刷新</button>
<script>
window.onload = random_A
function random_A() {
let str = ''
for(let i = 1i<=12i++){
str = str +String.fromCharCode(Math.floor(Math.random()*24+65))
}
document.getElementsByTagName("div")[0].innerText = str
}
</script>
</body>
</html>
效果:
代码仅供参考,我已经放到Github仓库,随时可以查看:
随机生成字符串
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)