getRandomItem
The getRandomItem()
function returns a random item from an array.
Parameters
Parameter | Type | Description |
---|---|---|
arr | Array | The array to select a random item from. |
Returns
Any
: A random item from the input array
Throws
Error
: If the input is not an array or if the array is empty
Example
const utils = require('utils-core.js');
const languages = ['javascript', 'python', 'html', 'css', 'c++'];
const randomItem = utils.random.getRandomItem(languages);
console.log(randomItem); // 'c++', for example
const numbers = [1, 2, 3, 4, 5, 6];
const randomNumber = utils.random.getRandomItem(numbers);
console.log(randomNumber); // 4, for example
See Also
- randomInRange: Generates a random number within a given range.
- Math.random(): Returns a random number between 0 (inclusive) and 1 (exclusive).