js生成随机数

// 获取从 1 到 10 的随机整数,取 0 的概率极小。
Math.ceil(Math.random()*10);     
// 可均衡获取 0 到 1 的随机整数。
Math.round(Math.random());       
// 可均衡获取 0 到 9 的随机整数。
Math.floor(Math.random()*10);    
// 基本均衡获取 0 到 10 的随机整数,其中获取最小值 0 和最大值 10 的几率少一半。
Math.round(Math.random()*10);    

js随机数不重复

let oldRandNum = -1;
// 获取随机数
let random = Math.floor(Math.random()*10);
// 避免重复
if(oldRandNum  == random){
while(oldRandNum  == random){
        random = Math.floor(Math.random()*10);
}
}
oldRandNum  = random;
console.log(random);

注意如果定在某个函数中时,oldRandNum 这个变量需要做成全局的。