1// Shuffle array
2const shuffled = array.sort(() => 0.5 - Math.random());
3
4// Get sub-array of first n elements after shuffled
5let selected = shuffled.slice(0, n);
6
1function getRandom(arr, n) {
2 var result = new Array(n),
3 len = arr.length,
4 taken = new Array(len);
5 if (n > len)
6 throw new RangeError("getRandom: more elements taken than available");
7 while (n--) {
8 var x = Math.floor(Math.random() * len);
9 result[n] = arr[x in taken ? taken[x] : x];
10 taken[x] = --len in taken ? taken[len] : len;
11 }
12 return result;
13}