2022-08-28 09:14:02 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
2022-08-28 09:14:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|