方法一 10位以内的随机数
1.使用Math.random()生成随机数
2.toString(36) 转换成36进制
3.然后使用slice()截取出来
Math.random().toString(36).slice(2,10)
方法二 对定义的数组字符集进行随机选取
let str = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function randomString(n) {
let res = "";
for(let i = 0; i < n ; i ++) {
let id = Math.ceil(Math.random()*35);
res += str[id];
}
return res;
}
console.log(randomString(6));