function getMultipleRandomElements(arr, num_elements) {
// 0.5 - Math.random()) = -0.5...0.5
// sort order will be set to increase or decrease
const shuffled = [...arr].sort(() => 0.5 - Math.random());
return shuffled.slice(0, num_elements);
}
const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
console.log(getMultipleRandomElements(arr, 2));
console.log(getMultipleRandomElements(arr, 3));
console.log(getMultipleRandomElements(arr, 4));
/*
run:
["c", "g"]
["c", "d", "e"]
["a", "c", "b", "e"]
*/