mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: update doc for random package
This commit is contained in:
@@ -31,6 +31,8 @@ import (
|
||||
- [RandNumeralOrLetter](#RandNumeralOrLetter)
|
||||
- [UUIdV4](#UUIdV4)
|
||||
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||
- [RandFloat](#RandFloat)
|
||||
- [RandFloats](#RandFloats)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -298,3 +300,55 @@ func main() {
|
||||
fmt.Println(result) //[0 4 7 1 5] (random)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RandFloat">RandFloat</span>
|
||||
|
||||
<p>生成随机float64数字,可以指定范围和精度</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func RandFloat(min, max float64, precision int) float64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/random"
|
||||
)
|
||||
|
||||
func main() {
|
||||
floatNumber := random.RandFloat(1.0, 5.0, 2)
|
||||
fmt.Println(floatNumber) //2.14 (random number)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RandFloats">RandFloats</span>
|
||||
|
||||
<p>生成随机float64数字切片,指定长度,范围和精度.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func RandFloats(n int, min, max float64, precision int) []float64
|
||||
```
|
||||
|
||||
<b>实例:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/random"
|
||||
)
|
||||
|
||||
func main() {
|
||||
floatNumbers := random.RandFloats(5, 1.0, 5.0, 2)
|
||||
fmt.Println(floatNumber) //[3.42 3.99 1.3 2.38 4.23] (random)
|
||||
}
|
||||
```
|
||||
@@ -32,6 +32,8 @@ import (
|
||||
- [RandSymbolChar](#RandSymbolChar)
|
||||
- [UUIdV4](#UUIdV4)
|
||||
- [RandUniqueIntSlice](#RandUniqueIntSlice)
|
||||
- [RandFloat](#RandFloat)
|
||||
- [RandFloats](#RandFloats)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -300,3 +302,55 @@ func main() {
|
||||
fmt.Println(result) //[0 4 7 1 5] (random)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RandFloat">RandFloat</span>
|
||||
|
||||
<p>Generate random float64 number between [min, max) with specific precision.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func RandFloat(min, max float64, precision int) float64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/random"
|
||||
)
|
||||
|
||||
func main() {
|
||||
floatNumber := random.RandFloat(1.0, 5.0, 2)
|
||||
fmt.Println(floatNumber) //2.14 (random number)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RandFloats">RandFloats</span>
|
||||
|
||||
<p>Generate a slice of random float64 numbers of length n that do not repeat.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func RandFloats(n int, min, max float64, precision int) []float64
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/random"
|
||||
)
|
||||
|
||||
func main() {
|
||||
floatNumbers := random.RandFloats(5, 1.0, 5.0, 2)
|
||||
fmt.Println(floatNumber) //[3.42 3.99 1.3 2.38 4.23] (random)
|
||||
}
|
||||
```
|
||||
@@ -163,8 +163,8 @@ func RandUniqueIntSlice(n, min, max int) []int {
|
||||
return nums
|
||||
}
|
||||
|
||||
// RandFloats generate a slice of random float64 of length n that do not repeat.
|
||||
// Play: https://go.dev/play/p/uBkRSOz73Ec
|
||||
// RandFloats generate a slice of random float64 numbers of length n that do not repeat.
|
||||
// Play: todo
|
||||
func RandFloats(n int, min, max float64, precision int) []float64 {
|
||||
nums := make([]float64, n)
|
||||
used := make(map[float64]struct{}, n)
|
||||
|
||||
@@ -3,6 +3,7 @@ package random
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ExampleRandInt() {
|
||||
@@ -151,3 +152,37 @@ func ExampleRandSymbolChar() {
|
||||
// true
|
||||
// 6
|
||||
}
|
||||
|
||||
func ExampleRandFloat() {
|
||||
pattern := `^[\d{1}.\d{2}]+$`
|
||||
reg := regexp.MustCompile(pattern)
|
||||
|
||||
num := RandFloat(1.0, 5.0, 2)
|
||||
|
||||
// check num is a random float in [1.0, 5.0)
|
||||
result1 := num >= 1.0 && num < 5.0
|
||||
result2 := reg.MatchString(strconv.FormatFloat(num, 'f', -1, 64))
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleRandFloats() {
|
||||
isInRange := true
|
||||
numbers := RandFloats(5, 1.0, 5.0, 2)
|
||||
for _, n := range numbers {
|
||||
isInRange = (n >= 1.0 && n < 5.0)
|
||||
}
|
||||
|
||||
fmt.Println(isInRange)
|
||||
fmt.Println(numbers)
|
||||
fmt.Println(len(numbers))
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// 5
|
||||
}
|
||||
|
||||
@@ -174,7 +174,6 @@ func TestRandFloat(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestRandFloat")
|
||||
|
||||
r1 := RandFloat(1.1, 10.1, 2)
|
||||
t.Log(r1)
|
||||
assert.GreaterOrEqual(r1, 5.0)
|
||||
assert.Less(r1, 10.1)
|
||||
|
||||
@@ -190,8 +189,10 @@ func TestRandFloats(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestRandFloats")
|
||||
|
||||
result := RandFloats(5, 1.0, 5.0, 2)
|
||||
t.Log("TestRandFloats result: ", result)
|
||||
numbers := RandFloats(5, 1.0, 5.0, 2)
|
||||
for _, n := range numbers {
|
||||
assert.Equal(true, (n >= 1.0 && n < 5.0))
|
||||
}
|
||||
|
||||
assert.Equal(len(result), 5)
|
||||
assert.Equal(len(numbers), 5)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user