dice.go 535 B

12345678910111213141516171819202122232425
  1. // Package dice contains common functions to generate random number.
  2. // It also initialize math/rand with the time in seconds at launch time.
  3. package dice // import "v2ray.com/core/common/dice"
  4. import (
  5. "math/rand"
  6. "time"
  7. )
  8. // Roll returns a non-negative number between 0 (inclusive) and n (exclusive).
  9. func Roll(n int) int {
  10. if n == 1 {
  11. return 0
  12. }
  13. return rand.Intn(n)
  14. }
  15. // RollUint16 returns a random uint16 value.
  16. func RollUint16() uint16 {
  17. return uint16(rand.Intn(65536))
  18. }
  19. func init() {
  20. rand.Seed(time.Now().Unix())
  21. }