shuffle.js

import shuffleInPlace from "./shuffle_in_place.js";

/**
 * [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) 是一种快速生成有限集合随机排列的方法。
 * 这是一个围绕 `shuffle_in_place` 的函数,保证不会修改输入。
 *
 * @param {Array} x 包含0个或多个数字的样本
 * @param {Function} [randomSource=Math.random] 一个可选的熵源,返回范围在 [0, 1) 之间的数字
 * @return {Array} 输入数组的随机排列版本
 * @example
 * var shuffled = shuffle([1, 2, 3, 4]);
 * shuffled; // = [2, 3, 1, 4] 或其他随机排列
 */
function shuffle(x, randomSource) {
    // 对原数组进行切片,以避免修改原数组
    const sample = x.slice();

    // 然后对浅拷贝的数组进行原地洗牌
    return shuffleInPlace(sample, randomSource);
}

export default shuffle;