diff options
Diffstat (limited to 'internal/api/utilities.go')
-rw-r--r-- | internal/api/utilities.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/internal/api/utilities.go b/internal/api/utilities.go index 508e341..4a816fc 100644 --- a/internal/api/utilities.go +++ b/internal/api/utilities.go @@ -72,3 +72,27 @@ func addRotLog(logs *[LogLength]string, last *int, parser chan string, value str } } } + +func randomSelect[T any](src []T, n int) []T { + if n <= 0 || len(src) == 0 { + return nil + } + if n >= len(src) { + // Return a copy of the whole slice + cpy := make([]T, len(src)) + copy(cpy, src) + return cpy + } + + // Make a copy so we don't modify the original slice + tmp := make([]T, len(src)) + copy(tmp, src) + + // Shuffle the copy in‑place + rand.Shuffle(len(tmp), func(i, j int) { + tmp[i], tmp[j] = tmp[j], tmp[i] + }) + + // Return the first n elements of the shuffled slice + return tmp[:n] +} |