常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

44 lines
659 B

package helper
import (
"crypto/rand"
"math/big"
)
/**
* 获取int类型的区间随机数
*/
func GetRandInt(min, max int) (r int, err error) {
_r, err := GetRandInt64(int64(min), int64(max))
if err != nil {
return
}
r = int(_r)
return
}
/**
* 获取int64类型的区间随机数
*/
func GetRandInt64(min, max int64) (r int64, err error) {
if min == max {
r = min
return
}
if min > max {
max, min = min, max
}
result, err := rand.Int(rand.Reader, big.NewInt(max-min))
if err != nil {
return
}
r = result.Int64() + min
b, err := rand.Int(rand.Reader, big.NewInt(2))
if err == nil && b.Int64() == 1 {
r += 1
}
return
}