js随机数组的实现

            let arr = [1,2,3,4,5,6,67,8,8,9,0,10];
            let arr1 = [1,2,3,4,5,6,67,8,8,9,0,10];
            //随机算法1
            function shuffle(array){
                for(let i = array.length-1; i > 0; i--){
                    const randomIndex = Math.floor( Math.random() * (i+1));
                    swap(array,i,randomIndex);
                }
                return array;
            }
            function swap(array,i,j){
                let tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
            let tmp1 = shuffle(arr);
            console.log(tmp1)
            
            //随机算法2
            arr1.sort(()=>{
                return Math.random() - 0.5
            })
            console.log(arr1);

 

posted @ 2020-09-09 08:30  放学别跑啊  阅读(275)  评论(0编辑  收藏  举报