生成 0-1 之间的随机数

Math.random()

生成 0-x 之间的随机整数:

Math.round(Math.random()*x)

生成 min-max 之间的随机整数:

Math.round(Math.random()*(max-min)+min)

生成N位随机数

/**
 * 函数--生成N位随机数
 * @param {*} N 数字的长度
 */
function randomNum(N) {
  return String(parseInt(Math.random() * Math.pow(10, N)) + Math.pow(10, N)).substring(
    1,
    N + 1
  );
}

生成随机id

(Math.random() + new Date().getTime()).toString(32).slice(0,8)

得到8位不重复的随机id ‘1h1obpbd’

生成随机颜色

//随机RGB颜色-方法1
function   getColor () {
     var i, rgb = [];
     for (i = 0; i< 3; i++) {
         rgb[i] = Math.round(255 * Math.random());
     }
     return 'rgb(' + rgb.join(',') + ')';
},
    
//随机RGB颜色-方法2
function rgb(){
  const r = Math.floor(Math.random()*256);
  const g = Math.floor(Math.random()*256);
  const b = Math.floor(Math.random()*256);
  return `rgb(${r},${g},${b})`;
}

//随机十六进制颜色
function color16(){
  const r = Math.floor(Math.random()*256);
  const g = Math.floor(Math.random()*256);
  const b = Math.floor(Math.random()*256);
  const color = `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
  return color;
}


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6