function sampleWithReplacement(x, n, randomSource) {
    if (x.length === 0) {
        return [];
    }

    // a custom random number source can be provided if you want to use
    // a fixed seed or another random number generator, like
    // [random-js](https://www.npmjs.org/package/random-js)
    randomSource = randomSource || Math.random;

    const length = x.length;
    const sample = [];

    for (let i = 0; i < n; i++) {
        const index = Math.floor(randomSource() * length);

        sample.push(x[index]);
    }

    return sample;
}
Sampling with replacement is a type of sampling that allows the same
item to be picked out of a population more than once.

@param {Array<*>} x an array of any kind of value
@param {number} n count of how many elements to take
@param {Function} [randomSource=Math.random] an optional entropy source that
returns numbers between 0 inclusive and 1 exclusive: the range [0, 1)
@return {Array} n sampled items from the population
@example
var values = [1, 2, 3, 4];
sampleWithReplacement(values, 2); // returns 2 random values, like [2, 4];