1 var baseClamp = require('./_baseClamp'),
2 baseRandom = require('./_baseRandom'),
3 toArray = require('./toArray'),
4 toInteger = require('./toInteger');
7 * Gets `n` random elements at unique keys from `collection` up to the
8 * size of `collection`.
12 * @category Collection
13 * @param {Array|Object} collection The collection to sample.
14 * @param {number} [n=0] The number of elements to sample.
15 * @returns {Array} Returns the random elements.
18 * _.sampleSize([1, 2, 3], 2);
21 * _.sampleSize([1, 2, 3], 4);
24 function sampleSize(collection, n) {
26 result = toArray(collection),
27 length = result.length,
28 lastIndex = length - 1;
30 n = baseClamp(toInteger(n), 0, length);
32 var rand = baseRandom(index, lastIndex),
35 result[rand] = result[index];
36 result[index] = value;
42 module.exports = sampleSize;