MCPcopy Index your code
hub / github.com/stemkoski/stemkoski.github.com / sample

Function sample

MathBox/mathbox-bundle.js:38974–38984  ·  view source on GitHub ↗

* Retrieves a random element or `n` random elements from a collection. * * @static * @memberOf _ * @category Collections * @param {Array|Object|string} collection The collection to sample. * @param {number} [n] The number of elements to sample. * @param- {Object} [

(collection, n, guard)

Source from the content-addressed store, hash-verified

38972 * // => [3, 1]
38973 */
38974 function sample(collection, n, guard) {
38975 if (collection && typeof collection.length != 'number') {
38976 collection = values(collection);
38977 }
38978 if (n == null || guard) {
38979 return collection ? collection[baseRandom(0, collection.length - 1)] : undefined;
38980 }
38981 var result = shuffle(collection);
38982 result.length = nativeMin(nativeMax(0, n), result.length);
38983 return result;
38984 }
38985
38986 /**
38987 * Creates an array of shuffled values, using a version of the Fisher-Yates

Callers

nothing calls this directly

Calls 3

valuesFunction · 0.85
baseRandomFunction · 0.85
shuffleFunction · 0.85

Tested by

no test coverage detected