From 1eef4d7aeebc0ec6f2039cb2eb750af2a038b14f Mon Sep 17 00:00:00 2001 From: cya <2081215119@qq.com> Date: Wed, 14 Aug 2024 16:15:44 +0800 Subject: [PATCH 1/2] feat: add RandFromGivenSlice function --- docs/api/packages/random.md | 30 +++++++++++++++++++++++++++++- docs/en/api/packages/random.md | 27 +++++++++++++++++++++++++++ random/random.go | 9 +++++++++ random/random_example_test.go | 18 ++++++++++++++++++ random/random_test.go | 23 +++++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) diff --git a/docs/api/packages/random.md b/docs/api/packages/random.md index 1b39d5b7..9aefd6c6 100644 --- a/docs/api/packages/random.md +++ b/docs/api/packages/random.md @@ -25,6 +25,7 @@ import ( - [RandBytes](#RandBytes) - [RandInt](#RandInt) - [RandString](#RandString) +- [RandFromGivenSlice](#RandFromGivenSlice) - [RandUpper](#RandUpper) - [RandLower](#RandLower) - [RandNumeral](#RandNumeral) @@ -80,7 +81,7 @@ func main() { func RandInt(min, max int) int ``` -示例:[运行](https://go.dev/play/p/pXyyAAI5YxD) +示例:[运行]() ```go package main @@ -122,6 +123,33 @@ func main() { } ``` +### RandFromGivenSlice + +
从给定切片中随机生成元素
+ +函数签名: + +```go +func RandFromGivenSlice[T any](slice []T) T +``` + +示例:[运行](https://go.dev/play/p/29QfOh0DVuh) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/random" +) + +func main() { + nicknames := []string{"张三", "李四", "王五", "赵六", "钱七"} + randElm := random.RandFromGivenSlice(nicknames) + fmt.Println(randElm) +} +``` + ### RandUpper生成给定长度的随机大写字母字符串
diff --git a/docs/en/api/packages/random.md b/docs/en/api/packages/random.md index f6cdbe55..173ee49c 100644 --- a/docs/en/api/packages/random.md +++ b/docs/en/api/packages/random.md @@ -121,6 +121,33 @@ func main() { } ``` +### RandFromGivenSlice + +Generate a random element from given slice.
+ +Signature: + +```go +func RandFromGivenSlice[T any](slice []T) T +``` + +Example:[Run]() + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/random" +) + +func main() { + randomSet := []any{"a", 8, "hello", true, 1.1} + randElm := random.RandFromGivenSlice(randomSet) + fmt.Println(randElm) +} +``` + ### RandUpperGenerate a random upper case string
diff --git a/random/random.go b/random/random.go index a87bb170..1a7b9707 100644 --- a/random/random.go +++ b/random/random.go @@ -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 { diff --git a/random/random_example_test.go b/random/random_example_test.go index 26b40269..bd1a728e 100644 --- a/random/random_example_test.go +++ b/random/random_example_test.go @@ -43,6 +43,24 @@ func ExampleRandString() { // 6 } +func ExampleRandFromGivenSlice() { + goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon", + "mango", "nectarine", "orange"} + + isInGoods := false + result := RandFromGivenSlice(goods) + for _, good := range goods { + if good == result { + isInGoods = true + break + } + } + fmt.Println(isInGoods) + + // Output: + // true +} + func ExampleRandUpper() { pattern := `^[A-Z]+$` reg := regexp.MustCompile(pattern) diff --git a/random/random_test.go b/random/random_test.go index 4da729e4..500b7eeb 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -269,6 +269,29 @@ func TestRandStringSlice(t *testing.T) { // }) } +func TestRandFromGivenSlice(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestRandFromGivenSlice") + + randomSet := []any{"a", 8, "王", true, 1.1} + result := RandFromGivenSlice(randomSet) + find := false + for _, v := range randomSet { + if v == result { + find = true + } + } + assert.Equal(true, find) + + emptyAnyRandomSet := []any{} + emptyAnyResult := RandFromGivenSlice(emptyAnyRandomSet) + assert.IsNil(emptyAnyResult) + + emptyIntRandomSet := []int{} + emtpyIntResult := RandFromGivenSlice(emptyIntRandomSet) + assert.Equal(0, emtpyIntResult) +} + func TestRandBool(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestRandBool") From 469ed93ba0ac3dfea5b977ed02c83a2ab8564bbe Mon Sep 17 00:00:00 2001 From: cya <2081215119@qq.com> Date: Wed, 14 Aug 2024 16:22:19 +0800 Subject: [PATCH 2/2] feat: add RandFromGivenSlice function --- docs/api/packages/random.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api/packages/random.md b/docs/api/packages/random.md index 9aefd6c6..c08ea148 100644 --- a/docs/api/packages/random.md +++ b/docs/api/packages/random.md @@ -81,7 +81,7 @@ func main() { func RandInt(min, max int) int ``` -示例:[运行]() +示例:[运行](https://go.dev/play/p/pXyyAAI5YxD) ```go package main @@ -133,7 +133,7 @@ func main() { func RandFromGivenSlice[T any](slice []T) T ``` -示例:[运行](https://go.dev/play/p/29QfOh0DVuh) +示例:[运行]() ```go package main