34 lines
681 B
JavaScript
34 lines
681 B
JavaScript
/**
|
|
* 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(choice(array));
|
|
}
|
|
}
|
|
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
|
|
} |