mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 09:12:28 +08:00
feat: add RandIntSlice function
This commit is contained in:
@@ -1167,7 +1167,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- **<big>UUIdV4</big>** : generate a random UUID of version 4 according to RFC 4122.
|
- **<big>UUIdV4</big>** : generate a random UUID of version 4 according to RFC 4122.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#UUIdV4)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#UUIdV4)]
|
||||||
[[play](https://go.dev/play/p/_Z9SFmr28ft)]
|
[[play](https://go.dev/play/p/_Z9SFmr28ft)]
|
||||||
- **<big>RandUniqueIntSlice</big>** : generate a slice of random int of length n that do not repeat.
|
- **<big>RandUniqueIntSlice</big>** : generate a slice of random int that do not repeat.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandUniqueIntSlice)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandUniqueIntSlice)]
|
||||||
[[play](https://go.dev/play/p/uBkRSOz73Ec)]
|
[[play](https://go.dev/play/p/uBkRSOz73Ec)]
|
||||||
- **<big>RandSymbolChar</big>** : Generate a random symbol char of specified length.
|
- **<big>RandSymbolChar</big>** : Generate a random symbol char of specified length.
|
||||||
@@ -1178,7 +1178,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandFloat)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandFloat)]
|
||||||
[[play](https://go.dev/play/p/zbD_tuobJtr)]
|
[[play](https://go.dev/play/p/zbD_tuobJtr)]
|
||||||
|
|
||||||
- **<big>RandFloats</big>** : Generate a slice of random float64 numbers of length n that do not repeat.
|
- **<big>RandFloats</big>** : Generate a slice of random float64 numbers that do not repeat.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandFloats)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandFloats)]
|
||||||
[[play](https://go.dev/play/p/I3yndUQ-rhh)]
|
[[play](https://go.dev/play/p/I3yndUQ-rhh)]
|
||||||
|
|
||||||
|
|||||||
@@ -1169,7 +1169,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- **<big>UUIdV4</big>** : 生成 UUID v4 字符串。
|
- **<big>UUIdV4</big>** : 生成 UUID v4 字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#UUIdV4)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#UUIdV4)]
|
||||||
[[play](https://go.dev/play/p/_Z9SFmr28ft)]
|
[[play](https://go.dev/play/p/_Z9SFmr28ft)]
|
||||||
- **<big>RandUniqueIntSlice</big>** : 生成一个不重复的长度为 n 的随机 int 切片。
|
- **<big>RandUniqueIntSlice</big>** : 生成一个不重复的随机int切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#RandUniqueIntSlice)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#RandUniqueIntSlice)]
|
||||||
[[play](https://go.dev/play/p/uBkRSOz73Ec)]
|
[[play](https://go.dev/play/p/uBkRSOz73Ec)]
|
||||||
- **<big>RandSymbolChar</big>** : 生成给定长度的随机符号字符串。
|
- **<big>RandSymbolChar</big>** : 生成给定长度的随机符号字符串。
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import (
|
|||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [RandSymbolChar](#RandSymbolChar)
|
- [RandSymbolChar](#RandSymbolChar)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
- [RandIntSlice](#RandIntSlice)
|
||||||
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||||
- [RandFloat](#RandFloat)
|
- [RandFloat](#RandFloat)
|
||||||
- [RandFloats](#RandFloats)
|
- [RandFloats](#RandFloats)
|
||||||
@@ -276,14 +277,40 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
### <span id="RandIntSlice">RandIntSlice</span>
|
||||||
|
|
||||||
<p>生成一个不重复的长度为n的随机int切片。</p>
|
<p>生成一个特定长度的随机int切片,数值范围[min, max)。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RandUniqueIntSlice(n, min, max int) []int
|
func RandIntSlice(length, min, max int) []int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := random.RandIntSlice(5, 0, 10)
|
||||||
|
fmt.Println(result) //[1 2 7 1 5] (random)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
||||||
|
|
||||||
|
<p>生成一个特定长度的,数值不重复的随机int切片,数值范围[min, max)。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUniqueIntSlice(length, min, max int) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
|
||||||
@@ -304,7 +331,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="RandFloat">RandFloat</span>
|
### <span id="RandFloat">RandFloat</span>
|
||||||
|
|
||||||
<p>生成随机float64数字,可以指定范围和精度。</p>
|
<p>生成一个随机float64数值,可以指定精度。数值范围[min, max)。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -330,7 +357,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="RandFloats">RandFloats</span>
|
### <span id="RandFloats">RandFloats</span>
|
||||||
|
|
||||||
<p>生成随机float64数字切片,指定长度,范围和精度.</p>
|
<p>生成一个特定长度的随机float64切片,可以指定数值精度。数值范围[min, max)。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import (
|
|||||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||||
- [RandSymbolChar](#RandSymbolChar)
|
- [RandSymbolChar](#RandSymbolChar)
|
||||||
- [UUIdV4](#UUIdV4)
|
- [UUIdV4](#UUIdV4)
|
||||||
|
- [RandIntSlice](#RandIntSlice)
|
||||||
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||||
- [RandFloat](#RandFloat)
|
- [RandFloat](#RandFloat)
|
||||||
- [RandFloats](#RandFloats)
|
- [RandFloats](#RandFloats)
|
||||||
@@ -276,15 +277,41 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="RandIntSlice">RandIntSlice</span>
|
||||||
|
|
||||||
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
<p>Generate a slice of random int. Number range in [min, max)</p>
|
||||||
|
|
||||||
<p>Generate a slice of random int of length n that do not repeat.</p>
|
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RandUniqueIntSlice(n, min, max int) []int
|
func RandIntSlice(length, min, max int) []int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := random.RandIntSlice(5, 0, 10)
|
||||||
|
fmt.Println(result) //[1 4 7 1 5] (random)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RandUniqueIntSlice">RandUniqueIntSlice</span>
|
||||||
|
|
||||||
|
<p>Generate a slice of random int of length that do not repeat. Number range in [min, max)</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RandUniqueIntSlice(length, min, max int) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
|
||||||
@@ -331,12 +358,12 @@ func main() {
|
|||||||
|
|
||||||
### <span id="RandFloats">RandFloats</span>
|
### <span id="RandFloats">RandFloats</span>
|
||||||
|
|
||||||
<p>Generate a slice of random float64 numbers of length n that do not repeat.</p>
|
<p>Generate a slice of random float64 numbers of length n that do not repeat. Number range in [min, max)</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func RandFloats(n int, min, max float64, precision int) []float64
|
func RandFloats(length int, min, max float64, precision int) []float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/I3yndUQ-rhh)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/I3yndUQ-rhh)</span></b>
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ func RandInt(min, max int) int {
|
|||||||
min, max = max, min
|
min, max = max, min
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if min == 0 && max == math.MaxInt {
|
||||||
|
return rand.Int()
|
||||||
|
}
|
||||||
|
|
||||||
return rand.Intn(max-min) + min
|
return rand.Intn(max-min) + min
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,19 +190,35 @@ 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
|
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.
|
// RandIntSlice generates a slice of random integers.
|
||||||
|
// The generated integers are between min and max (exclusive).
|
||||||
|
// Play: todo
|
||||||
|
func RandIntSlice(length, min, max int) []int {
|
||||||
|
if length <= 0 || min > max {
|
||||||
|
return []int{}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]int, length)
|
||||||
|
for i := range result {
|
||||||
|
result[i] = RandInt(min, max)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandUniqueIntSlice generate a slice of random int of length that do not repeat.
|
||||||
// Play: https://go.dev/play/p/uBkRSOz73Ec
|
// Play: https://go.dev/play/p/uBkRSOz73Ec
|
||||||
func RandUniqueIntSlice(n, min, max int) []int {
|
func RandUniqueIntSlice(length, min, max int) []int {
|
||||||
if min > max {
|
if min > max {
|
||||||
return []int{}
|
return []int{}
|
||||||
}
|
}
|
||||||
if n > max-min {
|
if length > max-min {
|
||||||
n = max - min
|
length = max - min
|
||||||
}
|
}
|
||||||
|
|
||||||
nums := make([]int, n)
|
nums := make([]int, length)
|
||||||
used := make(map[int]struct{}, n)
|
used := make(map[int]struct{}, length)
|
||||||
for i := 0; i < n; {
|
for i := 0; i < length; {
|
||||||
r := RandInt(min, max)
|
r := RandInt(min, max)
|
||||||
if _, use := used[r]; use {
|
if _, use := used[r]; use {
|
||||||
continue
|
continue
|
||||||
@@ -211,12 +231,12 @@ func RandUniqueIntSlice(n, min, max int) []int {
|
|||||||
return nums
|
return nums
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandFloats generate a slice of random float64 numbers of length n that do not repeat.
|
// RandFloats generate a slice of random float64 numbers of length that do not repeat.
|
||||||
// Play: https://go.dev/play/p/I3yndUQ-rhh
|
// Play: https://go.dev/play/p/I3yndUQ-rhh
|
||||||
func RandFloats(n int, min, max float64, precision int) []float64 {
|
func RandFloats(length int, min, max float64, precision int) []float64 {
|
||||||
nums := make([]float64, n)
|
nums := make([]float64, length)
|
||||||
used := make(map[float64]struct{}, n)
|
used := make(map[float64]struct{}, length)
|
||||||
for i := 0; i < n; {
|
for i := 0; i < length; {
|
||||||
r := RandFloat(min, max, precision)
|
r := RandFloat(min, max, precision)
|
||||||
if _, use := used[r]; use {
|
if _, use := used[r]; use {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -196,3 +196,27 @@ func TestRandFloats(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(len(numbers), 5)
|
assert.Equal(len(numbers), 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandIntSlice(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
assert := internal.NewAssert(t, "TestRandIntSlice")
|
||||||
|
|
||||||
|
t.Run("empty slice", func(t *testing.T) {
|
||||||
|
numbers := RandIntSlice(-1, 1, 5)
|
||||||
|
assert.Equal([]int{}, numbers)
|
||||||
|
|
||||||
|
numbers = RandIntSlice(0, 1, 5)
|
||||||
|
assert.Equal([]int{}, numbers)
|
||||||
|
|
||||||
|
numbers = RandIntSlice(3, 5, 1)
|
||||||
|
assert.Equal([]int{}, numbers)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("random int slice", func(t *testing.T) {
|
||||||
|
numbers := RandIntSlice(5, 1, 1)
|
||||||
|
assert.Equal([]int{1, 1, 1, 1, 1}, numbers)
|
||||||
|
|
||||||
|
numbers = RandIntSlice(5, 1, 5)
|
||||||
|
assert.Equal(5, len(numbers))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user