feat: add functions.
This commit is contained in:
24
rand/rand.go
Normal file
24
rand/rand.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
// RandomInt 根据区间产生随机数
|
||||
func RandomInt(min, max int) int {
|
||||
if min >= max {
|
||||
return max
|
||||
}
|
||||
return RANDOM.Intn(max-min) + min
|
||||
}
|
||||
|
||||
// RandomInt64 根据区间产生随机数
|
||||
func RandomInt64(min, max int64) int64 {
|
||||
if min >= max {
|
||||
return max
|
||||
}
|
||||
return RANDOM.Int63n(max-min) + min
|
||||
}
|
||||
17
rand/rand_test.go
Normal file
17
rand/rand_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRandomInt(t *testing.T) {
|
||||
for i := 0; i < 1000; i++ {
|
||||
n := RandomInt(1, 100)
|
||||
fmt.Println(n)
|
||||
assert.True(t, n >= 1)
|
||||
assert.True(t, n < 100)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user