_arraySampleSize.js 500 B

123456789101112131415161718
  1. var baseClamp = require('./_baseClamp'),
  2. copyArray = require('./_copyArray'),
  3. shuffleSelf = require('./_shuffleSelf');
  4. /**
  5. * A specialized version of `_.sampleSize` for arrays.
  6. *
  7. * @private
  8. * @param {Array} array The array to sample.
  9. * @param {number} n The number of elements to sample.
  10. * @returns {Array} Returns the random elements.
  11. */
  12. function arraySampleSize(array, n) {
  13. return shuffleSelf(copyArray(array), baseClamp(n, 0, array.length));
  14. }
  15. module.exports = arraySampleSize;