mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-12-23 05:59:49 +00:00
d7c5a0fc5f
During the roll, "duration" is converted to int, causing overflow on x32 processors. The value may be very small or negative, causing a panic in rand.Intn.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
// Package dice contains common functions to generate random number.
|
|
// It also initialize math/rand with the time in seconds at launch time.
|
|
package dice // import "github.com/xtls/xray-core/common/dice"
|
|
|
|
import (
|
|
"math/rand"
|
|
)
|
|
|
|
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
|
|
func Roll(n int) int {
|
|
if n == 1 {
|
|
return 0
|
|
}
|
|
return rand.Intn(n)
|
|
}
|
|
|
|
// RollInt63n returns a non-negative number between 0 (inclusive) and n (exclusive).
|
|
func RollInt63n(n int64) int64 {
|
|
if n == 1 {
|
|
return 0
|
|
}
|
|
return rand.Int63n(n)
|
|
}
|
|
|
|
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
|
|
func RollDeterministic(n int, seed int64) int {
|
|
if n == 1 {
|
|
return 0
|
|
}
|
|
return rand.New(rand.NewSource(seed)).Intn(n)
|
|
}
|
|
|
|
// RollUint16 returns a random uint16 value.
|
|
func RollUint16() uint16 {
|
|
return uint16(rand.Int63() >> 47)
|
|
}
|
|
|
|
func RollUint64() uint64 {
|
|
return rand.Uint64()
|
|
}
|
|
|
|
func NewDeterministicDice(seed int64) *DeterministicDice {
|
|
return &DeterministicDice{rand.New(rand.NewSource(seed))}
|
|
}
|
|
|
|
type DeterministicDice struct {
|
|
*rand.Rand
|
|
}
|
|
|
|
func (dd *DeterministicDice) Roll(n int) int {
|
|
if n == 1 {
|
|
return 0
|
|
}
|
|
return dd.Intn(n)
|
|
}
|