//生成一个整数随机数,并且范围为[min,max) test function randomInt(min,max){ if(min < 0 || max <0){ throw "不支持负数"; } var number = parseInt(Math.random().toString().substring(2)); return number%(max-min) + min; }
//生成count个不重复的随机数,并且范围为[scope_min,scope_max) function randomIntNoRepeat(count,scope_min,scope_max){ var numbers = []; if(scope_max - scope_min < count){//范围不正确 throw "范围不正确"; } for(var i = 0; i < count;i ++){ var number = randomInt(scope_min,scope_max); while(numbers.includes(number)){ number = randomInt(scope_min,scope_max); } numbers.push(number); } return numbers; }