/**
 * 洗牌算法
 * @param {array} list 
 * @returns 
 */
const shuffle = (list = []) => {
    let options = list.slice();
    let currentIndex = options.length;
    let temporary = null;
    let randomIndex = null;
    // 当还有剩余元素时
    while (currentIndex !== 0) {
        // 随机选取一个剩余的元素
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex--;

        // 交换当前元素与随机选取的元素
        temporary = options[currentIndex];
        options[currentIndex] = options[randomIndex];
        options[randomIndex] = temporary;
    }
    return options;
}

文章作者: CaptainTwo
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 CaptainTwo
NodeJs JavaScript NodeJs JavaScript
喜欢就支持一下吧