Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

随机生成字符串 #126

Open
Sunny-117 opened this issue Nov 3, 2022 · 4 comments
Open

随机生成字符串 #126

Sunny-117 opened this issue Nov 3, 2022 · 4 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@mengqiuleo
Copy link

function randomString(len){ //形参len, 需要生成的随机字符串的长度
  len = len || 32;
  const t = 'abcdefghigklmnopqrstuvwxyz123456798'; //模拟随机字符串库
  const tLen = t.length;
  let res = '';//存放最终生成的字符串
  for(let i=0; i<len; i++){
    res += t.charAt(Math.floor(Math.random()*tLen));
  }
  return res;
}
console.log(randomString(10));

@Tylermeek
Copy link

//  随机生成字符串

function getRandomStr(len) {
  let res = [];
  for (let i = 0; i < len; i++) {
    // unicode选取范围32到126
    let temp = getRndInteger(32, 126);
    res.push(temp)
  }
  return String.fromCharCode(...res)
}

// 以下函数返回 min(包含)~ max(包含)之间的数字:
function getRndInteger(min, max) {
  // 由于random含0不含1,所以直接加一即可实现双闭合
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomStr(6))

@HangHZhang
Copy link

function randomString(len, chars) {
    let res = []
    for (let i = 0; i < length; i++) {
        res.push(chars[Math.floor(Math.random() * chars.length)]) 
    }
    return res.join('')
}

@2239351258
Copy link

生成[0-9a-z]

Math.random().toString(36)
// '0.cieke0c5npq' 需要多长可以自己截取

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants