* Returns an array with a sample of two distinct elements from an array.
(array)
| 82 | * Returns an array with a sample of two distinct elements from an array. |
| 83 | */ |
| 84 | function sampleOfTwo(array) { |
| 85 | assert(array.length >= 2); |
| 86 | |
| 87 | const first = randInt(0, array.length - 1); |
| 88 | let second = randInt(1, array.length - 1); |
| 89 | if (first == second) { |
| 90 | // On a collision, we simulate a swap. |
| 91 | second = 0; |
| 92 | } |
| 93 | return [array[first], array[second]]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * As above for an arbitrary number of elements with a fast implementation |
no test coverage detected