* Gets a random element or `n` random elements from a collection. * * @static * @memberOf _ * @category Collection * @param {Array|Object|string} collection The collection to sample. * @param {number} [n] The number of elements to sample. * @param- {Object} [guard]
(collection, n, guard)
| 8420 | * // => [3, 1] |
| 8421 | */ |
| 8422 | function sample(collection, n, guard) { |
| 8423 | if (guard ? isIterateeCall(collection, n, guard) : n == null) { |
| 8424 | collection = toIterable(collection); |
| 8425 | var length = collection.length; |
| 8426 | return length > 0 ? collection[baseRandom(0, length - 1)] : undefined; |
| 8427 | } |
| 8428 | var result = shuffle(collection); |
| 8429 | result.length = nativeMin(n < 0 ? 0 : (+n || 0), result.length); |
| 8430 | return result; |
| 8431 | } |
| 8432 | |
| 8433 | /** |
| 8434 | * Creates an array of shuffled values, using a version of the |
no test coverage detected