feat: add functions.

This commit is contained in:
tx7do
2023-10-06 19:42:06 +08:00
parent a5ffa41fea
commit 03d09f2415
6 changed files with 525 additions and 0 deletions

24
rand/rand.go Normal file
View 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
View 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)
}
}