js 数组 随机排序

方法一:

        function getRandomInt(min, max) {
          return Math.floor(Math.random() * (max - min + 1) + min)
        }

        function shuffle(arr) {
          let _arr = arr.slice()
          for (let i = 0; i < _arr.length; i++) {
            let j = getRandomInt(0, i)
            let t = _arr[i]
            _arr[i] = _arr[j]
            _arr[j] = t
          }
          return _arr
        }
        console.log(shuffle([11,22,33,4,5,6]))

方法二:

arr.sort(function(){
            Math.random()>0.5?1:-1
        })

猜你喜欢

转载自www.cnblogs.com/hss-blog/p/9282699.html