picgo/corelib/captcha/random.go

25 lines
527 B
Go
Raw Normal View History

package captcha
import (
"math/rand"
"time"
)
// RandDigit 随机数
func RandDigit(max int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max)
}
// RandDigitRange 随机数范围
func RandDigitRange(max int, min int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max-min) + min
}
// RandChooseOne 数组随机选一个元素
func RandChooseOne[T any](reasons []T) T {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return reasons[r.Intn(len(reasons))]
}