function random(m, n) { return m + Math.floor(Math.random() * n); } function randomItem(arr, from = 0, to = arr.length) { const index = random(from, to); return { index, value: arr[index], }; } function shuffle(arr) { for (let i = arr.length; i > 0; i--) { const { index } = randomItem(arr, 0, i); [arr[index], arr[i - 1]] = [arr[i - 1], arr[index]]; } return arr; }
经典洗牌算法,随机排列一次