mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
feat: add RandSliceFromGivenSlice function (#236)
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
|||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
- [RandFromGivenSlice](#RandFromGivenSlice)
|
- [RandFromGivenSlice](#RandFromGivenSlice)
|
||||||
|
- [RandSliceFromGivenSlice](#RandSliceFromGivenSlice)
|
||||||
- [RandUpper](#RandUpper)
|
- [RandUpper](#RandUpper)
|
||||||
- [RandLower](#RandLower)
|
- [RandLower](#RandLower)
|
||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
@@ -150,6 +151,33 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandSliceFromGivenSlice">RandSliceFromGivenSlice</span>
|
||||||
|
|
||||||
|
<p>从给定切片中生成长度为 num 的随机切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandSliceFromGivenSlice[T any](slice []T, num int, repeatable bool) []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() {
|
||||||
|
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon","mango", "nectarine", "orange"}
|
||||||
|
chosen3goods := random.RandSliceFromGivenSlice(goods, 3, false)
|
||||||
|
fmt.Println(chosen3goods)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="RandUpper">RandUpper</span>
|
### <span id="RandUpper">RandUpper</span>
|
||||||
|
|
||||||
<p>生成给定长度的随机大写字母字符串</p>
|
<p>生成给定长度的随机大写字母字符串</p>
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ import (
|
|||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [RandFromGivenSlice](#RandFromGivenSlice)
|
||||||
|
- [RandSliceFromGivenSlice](#RandSliceFromGivenSlice)
|
||||||
- [RandUpper](#RandUpper)
|
- [RandUpper](#RandUpper)
|
||||||
- [RandLower](#RandLower)
|
- [RandLower](#RandLower)
|
||||||
- [RandNumeral](#RandNumeral)
|
- [RandNumeral](#RandNumeral)
|
||||||
@@ -148,6 +150,33 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandSliceFromGivenSlice">RandSliceFromGivenSlice</span>
|
||||||
|
|
||||||
|
<p>Generate a random slice of length num from given slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandSliceFromGivenSlice[T any](slice []T, num int, repeatable bool) []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() {
|
||||||
|
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon", "mango", "nectarine", "orange"}
|
||||||
|
chosen3goods := random.RandSliceFromGivenSlice(goods, 3, false)
|
||||||
|
fmt.Println(chosen3goods)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="RandUpper">RandUpper</span>
|
### <span id="RandUpper">RandUpper</span>
|
||||||
|
|
||||||
<p>Generate a random upper case string</p>
|
<p>Generate a random upper case string</p>
|
||||||
|
|||||||
@@ -186,6 +186,7 @@ func RandStringSlice(charset string, sliceLen, strLen int) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RandFromGivenSlice generate a random element from given slice.
|
// RandFromGivenSlice generate a random element from given slice.
|
||||||
|
// Play: todo
|
||||||
func RandFromGivenSlice[T any](slice []T) T {
|
func RandFromGivenSlice[T any](slice []T) T {
|
||||||
if len(slice) == 0 {
|
if len(slice) == 0 {
|
||||||
var zero T
|
var zero T
|
||||||
@@ -194,6 +195,35 @@ func RandFromGivenSlice[T any](slice []T) T {
|
|||||||
return slice[rand.Intn(len(slice))]
|
return slice[rand.Intn(len(slice))]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandSliceFromGivenSlice generate a random slice of length num from given slice.
|
||||||
|
// - If repeatable is true, the generated slice may contain duplicate elements.
|
||||||
|
//
|
||||||
|
// Play: todo
|
||||||
|
func RandSliceFromGivenSlice[T any](slice []T, num int, repeatable bool) []T {
|
||||||
|
if num <= 0 || len(slice) == 0 {
|
||||||
|
return slice
|
||||||
|
}
|
||||||
|
|
||||||
|
if !repeatable && num > len(slice) {
|
||||||
|
num = len(slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]T, num)
|
||||||
|
if repeatable {
|
||||||
|
for i := range result {
|
||||||
|
result[i] = slice[rand.Intn(len(slice))]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
shuffled := make([]T, len(slice))
|
||||||
|
copy(shuffled, slice)
|
||||||
|
rand.Shuffle(len(shuffled), func(i, j int) {
|
||||||
|
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
|
||||||
|
})
|
||||||
|
result = shuffled[:num]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// RandUpper generate a random upper case string of specified length.
|
// RandUpper generate a random upper case string of specified length.
|
||||||
// Play: https://go.dev/play/p/29QfOh0DVuh
|
// Play: https://go.dev/play/p/29QfOh0DVuh
|
||||||
func RandUpper(length int) string {
|
func RandUpper(length int) string {
|
||||||
|
|||||||
@@ -47,18 +47,15 @@ func ExampleRandFromGivenSlice() {
|
|||||||
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon",
|
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon",
|
||||||
"mango", "nectarine", "orange"}
|
"mango", "nectarine", "orange"}
|
||||||
|
|
||||||
isInGoods := false
|
|
||||||
result := RandFromGivenSlice(goods)
|
result := RandFromGivenSlice(goods)
|
||||||
for _, good := range goods {
|
fmt.Println(result)
|
||||||
if good == result {
|
}
|
||||||
isInGoods = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println(isInGoods)
|
|
||||||
|
|
||||||
// Output:
|
func ExampleRandSliceFromGivenSlice() {
|
||||||
// true
|
goods := []string{"apple", "banana", "cherry", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon",
|
||||||
|
"mango", "nectarine", "orange"}
|
||||||
|
chosen3goods := RandSliceFromGivenSlice(goods, 3, false)
|
||||||
|
fmt.Println(chosen3goods)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRandUpper() {
|
func ExampleRandUpper() {
|
||||||
|
|||||||
@@ -292,6 +292,46 @@ func TestRandFromGivenSlice(t *testing.T) {
|
|||||||
assert.Equal(0, emtpyIntResult)
|
assert.Equal(0, emtpyIntResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandSliceFromGivenSlice(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestRandSliceFromGivenSlice")
|
||||||
|
|
||||||
|
randomSet := []any{"a", 8, "王", true, 1.1}
|
||||||
|
repeatableResult := RandSliceFromGivenSlice(randomSet, 8, true)
|
||||||
|
assert.Equal(8, len(repeatableResult))
|
||||||
|
unrepeatableResult := RandSliceFromGivenSlice(randomSet, 8, false)
|
||||||
|
assert.Equal(len(randomSet), len(unrepeatableResult))
|
||||||
|
|
||||||
|
var findCount int
|
||||||
|
for _, v := range repeatableResult {
|
||||||
|
for _, vv := range randomSet {
|
||||||
|
if v == vv {
|
||||||
|
findCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(8, findCount)
|
||||||
|
findCount = 0
|
||||||
|
|
||||||
|
for _, v := range unrepeatableResult {
|
||||||
|
for _, vv := range randomSet {
|
||||||
|
if v == vv {
|
||||||
|
findCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Equal(len(randomSet), findCount)
|
||||||
|
|
||||||
|
emptyAnyRandomSet := []any{}
|
||||||
|
emptyAnyResult := RandSliceFromGivenSlice(emptyAnyRandomSet, 3, true)
|
||||||
|
assert.Equal([]any{}, emptyAnyResult)
|
||||||
|
|
||||||
|
emptyIntRandomSet := []int{}
|
||||||
|
emtpyIntResult := RandSliceFromGivenSlice(emptyIntRandomSet, 3, true)
|
||||||
|
assert.Equal([]int{}, emtpyIntResult)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestRandBool(t *testing.T) {
|
func TestRandBool(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
assert := internal.NewAssert(t, "TestRandBool")
|
assert := internal.NewAssert(t, "TestRandBool")
|
||||||
|
|||||||
Reference in New Issue
Block a user