diginext-utils
Version:
README.md
20 lines • 544 B
JavaScript
/**
* Returns a random index from an array.
*
* @template T - The type of elements in the array
* @param array - The array to get a random index from
* @returns A random index, or -1 if array is empty
*
* @example
* ```ts
* randomIndex([1, 2, 3, 4, 5]); // Random number between 0 and 4
* randomIndex([]); // -1
* ```
*/
export function randomIndex(array) {
if (!Array.isArray(array) || array.length === 0) {
return -1;
}
return Math.floor(Math.random() * array.length);
}
//# sourceMappingURL=randomIndex.js.map