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

<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位,注意指定的获取位数不要低于数组中的个数,不然要出问题。

添加新评论