2016-02-03 21:49:38 +00:00
|
|
|
// Package dice contains common functions to generate random number.
|
|
|
|
// It also initialize math/rand with the time in seconds at launch time.
|
2018-04-03 20:34:59 +00:00
|
|
|
package dice // import "v2ray.com/core/common/dice"
|
2016-01-21 21:45:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2016-01-25 10:47:22 +00:00
|
|
|
"time"
|
2016-01-21 21:45:44 +00:00
|
|
|
)
|
|
|
|
|
2016-02-03 21:49:38 +00:00
|
|
|
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
|
2016-01-21 21:45:44 +00:00
|
|
|
func Roll(n int) int {
|
|
|
|
if n == 1 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return rand.Intn(n)
|
|
|
|
}
|
2016-01-25 10:47:22 +00:00
|
|
|
|
2017-04-27 09:54:15 +00:00
|
|
|
// RollUint16 returns a random uint16 value.
|
|
|
|
func RollUint16() uint16 {
|
2017-02-14 21:29:44 +00:00
|
|
|
return uint16(rand.Intn(65536))
|
|
|
|
}
|
|
|
|
|
2016-01-25 10:47:22 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|