soundbirder/utils/choices.js
2023-07-29 18:21:40 +02:00

34 lines
669 B
JavaScript
Executable File

/**
* Get a random selection from an array.
*
* @param {Array} array
* @param {Number} number
* @returns `number` random elements from the array
*/
function choices(array, number) {
const result = [];
for (let i = 0; i < number; i++) {
let c = choice(array);
if (result.includes(c)) {
i--;
} else {
result.push(c);
}
}
return result;
}
/**
* Get a random element from an array.
*
* @param {Array} array
* @returns a random element from the array
*/
function choice(array) {
return array[Math.floor(Math.random() * array.length)];
}
module.exports = {
choices,
choice
}