The Wayback Machine - https://web.archive.org/web/20201220222209/https://github.com/lodash/lodash/blob/master/sample.js
Skip to content
Permalink
master
Go to file
1 contributor

Users who have contributed to this file

18 lines (17 sloc) 396 Bytes
/**
* Gets a random element from `array`.
*
* @since 2.0.0
* @category Array
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
* @example
*
* sample([1, 2, 3, 4])
* // => 2
*/
function sample(array) {
const length = array == null ? 0 : array.length
return length ? array[Math.floor(Math.random() * length)] : undefined
}
export default sample
You can’t perform that action at this time.