1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 00:02:28 +08:00

feat: add RandFromGivenSlice function (#235)

* feat: add RandFromGivenSlice function

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

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 {