# 生成验证码

1.生成 1000 个验证码,且不重复;每个验证码由 4 个数字组成。
2.如果每个验证码的相邻数字不重复应该怎么写

function gen(total, len) {
  let totalNums = total || 1000;
  const numLen = len || 4;
  const map = {};
  const res = [];

  function random(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1) + min);
  }

  while (totalNums) {
    let curNum = '';
    let curLen = numLen;
    while (curLen) {
      let num = random(0, 9);
      while (num + '' === curNum[numLen - curLen - 1]) {
        num = random(0, 9);
      }

      curNum += num;
      curLen--;
    }
    if (!map[curNum]) {
      map[curNum] = curNum;
      res.push(curNum);
      totalNums--;
    }
  }
  return res;
}
console.log(gen(10, 4));