feat: add RandFromGivenSlice function (#235)

* feat: add RandFromGivenSlice function

* feat: add RandFromGivenSlice function
This commit is contained in:
残念 authored and GitHub committed 2024-08-14 19:36:12 +08:00
1 parent a360372aa9
commit 7b4e060f85
5 files changed
+105

No files matched your search

+9
View File
@@ -185,6 +185,15 @@ func RandStringSlice(charset string, sliceLen, strLen int) []string {
return result
}
// RandFromGivenSlice generate a random element from given slice.
func RandFromGivenSlice[T any](slice []T) T {
if len(slice) == 0 {
var zero T
return zero
}
return slice[rand.Intn(len(slice))]
}
// RandUpper generate a random upper case string of specified length.
// Play: https://go.dev/play/p/29QfOh0DVuh
func RandUpper(length int) string {