2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
random.go 920 Bytes
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-04 11:45 . upd:目录结构调整
/**
* @Author Awen
* @Date 2024/06/01
* @Email wengaolng@gmail.com
**/
package random
import (
rand2 "crypto/rand"
"math"
"math/big"
"math/rand"
"time"
)
var rnd = newRnd()
// newRnd .
func newRnd() *rand.Rand {
var seed = time.Now().UnixNano()
var src = rand.NewSource(seed)
return rand.New(src)
}
// Rand63n .
func Rand63n(ri int64) {
rnd.Int63n(ri)
}
// Rand31n .
func Rand31n(ri int32) {
rnd.Int31n(ri)
}
// Perm .
func Perm(n int) []int {
return rand.Perm(n)
}
// RandInt is the safe random number of the interval [n, m]
func RandInt(min, max int) int {
if min > max {
return max
}
if min < 0 {
f64Min := math.Abs(float64(min))
i64Min := int(f64Min)
result, _ := rand2.Int(rand2.Reader, big.NewInt(int64(max+1+i64Min)))
return int(result.Int64() - int64(i64Min))
}
result, _ := rand2.Int(rand2.Reader, big.NewInt(int64(max-min+1)))
return int(int64(min) + result.Int64())
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.0.2

搜索帮助