js中通用随机洗牌方法
作者:互联网
export function shuffle(source) {
const arr = source.slice()
for (let i = 0; i < arr.length; i++) {
const j = getRandomInt(i)
swap(arr, i, j)
}
return arr
}
function getRandomInt(max) { return Math.floor(Math.random() * (max + 1)) }
function swap(arr, i, j) { const t = arr[i] arr[i] = arr[j] arr[j] = t }
function getRandomInt(max) { return Math.floor(Math.random() * (max + 1)) }
function swap(arr, i, j) { const t = arr[i] arr[i] = arr[j] arr[j] = t }
标签:function,arr,const,max,洗牌,js,source,随机,Math 来源: https://www.cnblogs.com/guangzhou11/p/14872775.html