1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +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

@@ -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)

View File

@@ -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
}

View File

@@ -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)
}