js 获取随机字符串
a为所需字符串的位数
如若位数不一定,可使用Math.random获取随机位数。
例如:
let num = Math.ceil(Math.random() * 8 + 10);//获取的是10-18之间的随机数。
function getstr(a) {
var d,
e,
b = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”,
c = “”;
for (d = 0; a > d; d += 1)
(e = Math.random() * b.length), (e = Math.floor(e)), (c += b.charAt(e));
return c;
}
代码片
function getstr(a) {
var d,
e,
b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
c = "";
for (d = 0; a > d; d += 1)
(e = Math.random() * b.length), (e = Math.floor(e)), (c += b.charAt(e));
return c;
}
let num = Math.ceil(Math.random() * 8 + 10);
getstr(num);//num==11;airH87HWfdh
getstr(4);//8jhL