Skip to content

Commit

Permalink
feat: add RandFromGivenSlice function (#235)
Browse files Browse the repository at this point in the history
* feat: add RandFromGivenSlice function

* feat: add RandFromGivenSlice function
  • Loading branch information
cannian1 authored Aug 14, 2024
1 parent a360372 commit 7b4e060
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/api/packages/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
- [RandBytes](#RandBytes)
- [RandInt](#RandInt)
- [RandString](#RandString)
- [RandFromGivenSlice](#RandFromGivenSlice)
- [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
Expand Down Expand Up @@ -122,6 +123,33 @@ func main() {
}
```

### <span id="RandFromGivenSlice">RandFromGivenSlice</span>

<p>从给定切片中随机生成元素</p>

<b>函数签名:</b>

```go
func RandFromGivenSlice[T any](slice []T) T
```

<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>

```go
package main

import (
"fmt"
"github.com/duke-git/lancet/v2/random"
)

func main() {
nicknames := []string{"张三", "李四", "王五", "赵六", "钱七"}
randElm := random.RandFromGivenSlice(nicknames)
fmt.Println(randElm)
}
```

### <span id="RandUpper">RandUpper</span>

<p>生成给定长度的随机大写字母字符串</p>
Expand Down
27 changes: 27 additions & 0 deletions docs/en/api/packages/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ func main() {
}
```

### <span id="RandFromGivenSlice">RandFromGivenSlice</span>

<p>Generate a random element from given slice.</p>

<b>Signature:</b>

```go
func RandFromGivenSlice[T any](slice []T) T
```

<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>

```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)
}
```

### <span id="RandUpper">RandUpper</span>

<p>Generate a random upper case string</p>
Expand Down
9 changes: 9 additions & 0 deletions random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions random/random_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7b4e060

Please sign in to comment.