1 var iArray = []; 2 function getRandom(istart, iend) { 3 var iChoice = iend - istart + 1; //加1是為了取到100 4 var res = Math.floor(Math.random() * iChoice + istart); //[0,90]+10 5 return res; 6 } 7 for (var i = 0; i < 10; i++) { 8 iArray.push(getRandom(10, 100)); 9 } 10 iArray.sort(function (a, b) { 11 return a > b; 12 }); 13 console.log(iArray); 14 15 16 鏈接:https://www.jianshu.com/p/a52b1a67e754
如果這10個數字還得保證不相同呢?
