Y-Panel/util/common/random.go

19 lines
328 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common
import (
"crypto/rand"
"math/big"
)
// RandomInt 返回一个 0 .. max-1 之间的随机整数(使用 crypto/rand
func RandomInt(max int) int {
if max <= 0 {
return 0
}
n, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
return 0
}
return int(n.Int64())
}