* 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)
| 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 |
nothing calls this directly
no test coverage detected