let arr = []; function test() { if(arr.length!==5) { let math = randomNum(2,32); if(!arr.includes(math)) { arr.push(math); } console.log(`${arr.length}:`,arr) test(); } } test(); //生成从minNum到maxNum的随机数 function randomNum(minNum,maxNum){ switch(arguments.length){ case 1: return parseInt(Math.random()*minNum+1,10); break; case 2: return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); break; default: return 0; break; } } // console.log(randomNum(2,32)) console.log('-------')