soundbirder/utils/choices.js

34 lines
669 B
JavaScript
Raw Normal View History

/**
* 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 {
2022-08-29 10:38:42 +02:00
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
}