diff --git a/docs/api/packages/random.md b/docs/api/packages/random.md index e68e2af..346bac5 100644 --- a/docs/api/packages/random.md +++ b/docs/api/packages/random.md @@ -31,6 +31,8 @@ import ( - [RandNumeralOrLetter](#RandNumeralOrLetter) - [UUIdV4](#UUIdV4) - [RandUniqueIntSlice](#RandUniqueIntSlice) +- [RandFloat](#RandFloat) +- [RandFloats](#RandFloats)
@@ -298,3 +300,55 @@ func main() { fmt.Println(result) //[0 4 7 1 5] (random) } ``` + +### RandFloat + +生成随机float64数字,可以指定范围和精度
+ +函数签名: + +```go +func RandFloat(min, max float64, precision int) float64 +``` + +实例: + +```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) +} +``` + +### RandFloats + +生成随机float64数字切片,指定长度,范围和精度.
+ +函数签名: + +```go +func RandFloats(n int, min, max float64, precision int) []float64 +``` + +实例: + +```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) +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/random.md b/docs/en/api/packages/random.md index 74381cd..b734608 100644 --- a/docs/en/api/packages/random.md +++ b/docs/en/api/packages/random.md @@ -32,6 +32,8 @@ import ( - [RandSymbolChar](#RandSymbolChar) - [UUIdV4](#UUIdV4) - [RandUniqueIntSlice](#RandUniqueIntSlice) +- [RandFloat](#RandFloat) +- [RandFloats](#RandFloats) @@ -300,3 +302,55 @@ func main() { fmt.Println(result) //[0 4 7 1 5] (random) } ``` + +### RandFloat + +Generate random float64 number between [min, max) with specific precision.
+ +Signature: + +```go +func RandFloat(min, max float64, precision int) float64 +``` + +Example: + +```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) +} +``` + +### RandFloats + +Generate a slice of random float64 numbers of length n that do not repeat.
+ +Signature: + +```go +func RandFloats(n int, min, max float64, precision int) []float64 +``` + +Example: + +```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) +} +``` \ No newline at end of file diff --git a/random/random.go b/random/random.go index 05a34ad..fe155dd 100644 --- a/random/random.go +++ b/random/random.go @@ -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) diff --git a/random/random_example_test.go b/random/random_example_test.go index 341585f..85c157f 100644 --- a/random/random_example_test.go +++ b/random/random_example_test.go @@ -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 +} diff --git a/random/random_test.go b/random/random_test.go index 0f0efb7..57a7b3e 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -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) }