1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 17:22:27 +08:00

doc: update doc for random package

This commit is contained in:
dudaodong
2023-12-08 11:57:39 +08:00
parent 6e5b67bee7
commit a995db445a
5 changed files with 150 additions and 6 deletions

View File

@@ -31,6 +31,8 @@ import (
- [RandNumeralOrLetter](#RandNumeralOrLetter) - [RandNumeralOrLetter](#RandNumeralOrLetter)
- [UUIdV4](#UUIdV4) - [UUIdV4](#UUIdV4)
- [RandUniqueIntSlice](#RandUniqueIntSlice) - [RandUniqueIntSlice](#RandUniqueIntSlice)
- [RandFloat](#RandFloat)
- [RandFloats](#RandFloats)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -298,3 +300,55 @@ func main() {
fmt.Println(result) //[0 4 7 1 5] (random) 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)
}
```

View File

@@ -32,6 +32,8 @@ import (
- [RandSymbolChar](#RandSymbolChar) - [RandSymbolChar](#RandSymbolChar)
- [UUIdV4](#UUIdV4) - [UUIdV4](#UUIdV4)
- [RandUniqueIntSlice](#RandUniqueIntSlice) - [RandUniqueIntSlice](#RandUniqueIntSlice)
- [RandFloat](#RandFloat)
- [RandFloats](#RandFloats)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -300,3 +302,55 @@ func main() {
fmt.Println(result) //[0 4 7 1 5] (random) 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)
}
```

View File

@@ -163,8 +163,8 @@ func RandUniqueIntSlice(n, min, max int) []int {
return nums return nums
} }
// RandFloats generate a slice of random float64 of length n that do not repeat. // RandFloats generate a slice of random float64 numbers of length n that do not repeat.
// Play: https://go.dev/play/p/uBkRSOz73Ec // Play: todo
func RandFloats(n int, min, max float64, precision int) []float64 { func RandFloats(n int, min, max float64, precision int) []float64 {
nums := make([]float64, n) nums := make([]float64, n)
used := make(map[float64]struct{}, n) used := make(map[float64]struct{}, n)

View File

@@ -3,6 +3,7 @@ package random
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"strconv"
) )
func ExampleRandInt() { func ExampleRandInt() {
@@ -151,3 +152,37 @@ func ExampleRandSymbolChar() {
// true // true
// 6 // 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
}

View File

@@ -174,7 +174,6 @@ func TestRandFloat(t *testing.T) {
assert := internal.NewAssert(t, "TestRandFloat") assert := internal.NewAssert(t, "TestRandFloat")
r1 := RandFloat(1.1, 10.1, 2) r1 := RandFloat(1.1, 10.1, 2)
t.Log(r1)
assert.GreaterOrEqual(r1, 5.0) assert.GreaterOrEqual(r1, 5.0)
assert.Less(r1, 10.1) assert.Less(r1, 10.1)
@@ -190,8 +189,10 @@ func TestRandFloats(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestRandFloats") assert := internal.NewAssert(t, "TestRandFloats")
result := RandFloats(5, 1.0, 5.0, 2) numbers := RandFloats(5, 1.0, 5.0, 2)
t.Log("TestRandFloats result: ", result) for _, n := range numbers {
assert.Equal(true, (n >= 1.0 && n < 5.0))
}
assert.Equal(len(result), 5) assert.Equal(len(numbers), 5)
} }