This commit is contained in:
lu.bai
2022-11-24 22:47:24 +08:00
parent f6a161d52f
commit 71d0135d6f
3213 changed files with 583576 additions and 216 deletions

57
pkg/random/random.go Normal file
View File

@@ -0,0 +1,57 @@
package random
import (
"encoding/hex"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func GetRandomStr(strLen int) (str string) {
baseStr := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i := 0; i < strLen; i++ {
index := rand.Intn(len(baseStr))
str += string(baseStr[index])
}
return str
}
func GetRandomByte(len int) []byte {
ret := make([]byte, 0)
for i := 0; i < len; i++ {
r := uint8(rand.Intn(256))
ret = append(ret, r)
}
return ret
}
func GetRandomByteHexStr(len int) string {
return hex.EncodeToString(GetRandomByte(len))
}
func GetRandomInt32(min int32, max int32) int32 {
if max <= min {
return 0
}
r := rand.Int31n(max-min+1) + min
return r
}
func GetRandomFloat32(min float32, max float32) float32 {
if max <= min {
return 0.0
}
r := rand.Float32()*(max-min) + min
return r
}
func GetRandomFloat64(min float64, max float64) float64 {
if max <= min {
return 0.0
}
r := rand.Float64()*(max-min) + min
return r
}

11
pkg/random/random_test.go Normal file
View File

@@ -0,0 +1,11 @@
package random
import (
"fmt"
"testing"
)
func TestGetRandomStr(t *testing.T) {
str := GetRandomStr(16)
fmt.Println(str)
}