simple-statistics
Version:
26 lines (22 loc) • 950 B
JavaScript
import shuffleInPlace from "./shuffle_in_place.js";
/**
* A [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
* is a fast way to create a random permutation of a finite set. This is
* a function around `shuffle_in_place` that adds the guarantee that
* it will not modify its input.
*
* @param {Array} x sample of 0 or more numbers
* @param {Function} [randomSource=Math.random] an optional entropy source that
* returns numbers between 0 inclusive and 1 exclusive: the range [0, 1)
* @return {Array} shuffled version of input
* @example
* var shuffled = shuffle([1, 2, 3, 4]);
* shuffled; // = [2, 3, 1, 4] or any other random permutation
*/
function shuffle(x, randomSource) {
// slice the original array so that it is not modified
const sample = x.slice();
// and then shuffle that shallow-copied array, in place
return shuffleInPlace(sample, randomSource);
}
export default shuffle;