js从数字数组里随机获取N个不重复的数字

来自:互联网
时间:2020-04-15
阅读:
免费资源网 - https://freexyz.cn/
<script>
    var arr = [713,1032,1051,710,711,943,985,1003,1011,1029,1031,1033,1034,1052,1056,4192,6965,6993,6994,6995,6997,6998,6999,7000,7362,7364,7365,7367,7368,7371,7372,7373,7374,7375,7376,7377,7378,7379,7380,7381,7382,7383,7384,7386,7388];
    var out = [];
    var num = 12;
    while(out.length < num){
        var temp = (Math.random()*arr.length) >> 0;
        out.push(arr.splice(temp,1));
    }
    document.write(out);
</script>

arr是指定的数字数组,num是指定的12位,注意指定的获取位数不要低于数组中的个数,不然要出问题。

免费资源网 - https://freexyz.cn/
返回顶部
顶部