diff --git a/docs/random.md b/docs/random.md index 62506b1..eac6d82 100644 --- a/docs/random.md +++ b/docs/random.md @@ -30,6 +30,7 @@ import ( - [RandNumeral](#RandNumeral) - [RandNumeralOrLetter](#RandNumeralOrLetter) - [UUIdV4](#UUIdV4) +- [RandUniqueIntSlice](#RandUniqueIntSlice)
@@ -245,3 +246,30 @@ func main() { fmt.Println(uuid) } ``` + + +### RandUniqueIntSlice + +Generate a slice of random int of length n that do not repeat.
+ +Signature: + +```go +func RandUniqueIntSlice(n, min, max int) []int +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/random" +) + +func main() { + result := RandUniqueIntSlice(5, 0, 10) + fmt.Println(result) //[0 4 7 1 5] (random) +} +``` diff --git a/docs/random_zh-CN.md b/docs/random_zh-CN.md index af51a84..4cf38ec 100644 --- a/docs/random_zh-CN.md +++ b/docs/random_zh-CN.md @@ -30,6 +30,7 @@ import ( - [RandNumeral](#RandNumeral) - [RandNumeralOrLetter](#RandNumeralOrLetter) - [UUIdV4](#UUIdV4) +- [RandUniqueIntSlice](#RandUniqueIntSlice) @@ -245,3 +246,29 @@ func main() { fmt.Println(uuid) } ``` + +### RandUniqueIntSlice + +生成一个不重复的长度为n的随机int切片。
+ +函数签名: + +```go +func RandUniqueIntSlice(n, min, max int) []int +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/random" +) + +func main() { + result := RandUniqueIntSlice(5, 0, 10) + fmt.Println(result) //[0 4 7 1 5] (random) +} +``` diff --git a/random/random.go b/random/random.go index d551d74..698caf8 100644 --- a/random/random.go +++ b/random/random.go @@ -115,7 +115,8 @@ func UUIdV4() (string, error) { return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil } -// RandUniqueIntSlice generate a slice of random int of length n that do not repeat +// RandUniqueIntSlice generate a slice of random int of length n that do not repeat. +// Play: todo func RandUniqueIntSlice(n, min, max int) []int { if min > max { return []int{} diff --git a/random/random_example_test.go b/random/random_example_test.go index e2f6032..c5629f2 100644 --- a/random/random_example_test.go +++ b/random/random_example_test.go @@ -130,6 +130,7 @@ func ExampleRandUniqueIntSlice() { if len(result) == 5 { fmt.Println("ok") } + fmt.Println(result) // Output: // ok