mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 15:12:26 +08:00
feat: add RandFromGivenSlice function (#235)
* feat: add RandFromGivenSlice function * feat: add RandFromGivenSlice function
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user