let array = [1, 2, 3, 4, 5]
function shuffle(array) {
for(let i = array.length -1; i > 0; i--) {
let rowIndex = Math.floor(Math.random() * (i + 1));
swap(array, i, rowIndex);
}
}
function swap(array, i, rowIndex) {
let temp = array[rowIndex]
array[rowIndex] = array[i]
array[i] = temp
}
shuffle(array)
console.log(array)
Fisher-Yates 随机算法
最新推荐文章于 2021-12-12 21:30:19 发布