* Randomly sample a specified number of elements from an array
(items: T[], sampleSize: number)
| 120 | * Randomly sample a specified number of elements from an array |
| 121 | */ |
| 122 | function sampleItems<T>(items: T[], sampleSize: number): T[] { |
| 123 | const copy = [...items]; |
| 124 | for (let i = copy.length - 1; i > 0; i--) { |
| 125 | const j = Math.floor(Math.random() * (i + 1)); |
| 126 | [copy[i], copy[j]] = [copy[j], copy[i]]; |
| 127 | } |
| 128 | return copy.slice(0, sampleSize); |
| 129 | } |
no outgoing calls
no test coverage detected