random-item
Version:
Get a random item from an array
16 lines (12 loc) • 409 B
JavaScript
export default function randomItem(array) {
if (!Array.isArray(array)) {
throw new TypeError('Expected an array');
}
return array[Math.floor(Math.random() * array.length)];
}
randomItem.multiple = (array, count) => {
if (!(Number.isInteger(count) && count >= 0)) {
throw new TypeError('Expected a non-negative integer');
}
return [...Array.from({length: count})].map(() => randomItem(array));
};