将数组随机打乱
非随机排序
// ES6:
let arr = [1, 2, 3, 4, 5]
let newArr = (arr) => arr.sort(() => Math.random() - 0.5)
Fisher–Yates 洗牌算法
function shuffle(arr) {
let i = arr.length
while (i) {
let j = Math.floor(Math.random() * i--)
;[arr[i], arr[j]] = [arr[j], arr[i]]
}
}