生成隨機數
function randomNum(min = 0, max = 100, len = 0) {
return Number((min + (max - min) * Math.random()).toFixed(len));
}
function randomNumTest() {
setInterval(() => {
console.log(randomNum(0, 100, 2));
}, 100);
}
randomNumTest();
生成隨機字符串
function randomStr(len = 8) {
let str = '';
let list = '0123456789abcdefghijklmnopqrstuvwxyz';
for (let i = 0; i < len; i++) {
let index = randomNum(0, 35);
let word = list[index];
if (isNaN(word) && randomNum() < 50) {
word = word.toUpperCase();
}
str += word;
}
return str;
}
function randomStrTest() {
setInterval(() => {
console.log(randomStr(36));
}, 100);
}
randomStrTest();