math.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package randutil
  2. import (
  3. mrand "math/rand" // used for non-crypto unique ID and random port selection
  4. "sync"
  5. "time"
  6. )
  7. // MathRandomGenerator is a random generator for non-crypto usage.
  8. type MathRandomGenerator interface {
  9. // Intn returns random integer within [0:n).
  10. Intn(n int) int
  11. // Uint32 returns random 32-bit unsigned integer.
  12. Uint32() uint32
  13. // Uint64 returns random 64-bit unsigned integer.
  14. Uint64() uint64
  15. // GenerateString returns ranom string using given set of runes.
  16. // It can be used for generating unique ID to avoid name collision.
  17. //
  18. // Caution: DO NOT use this for cryptographic usage.
  19. GenerateString(n int, runes string) string
  20. }
  21. type mathRandomGenerator struct {
  22. r *mrand.Rand
  23. mu sync.Mutex
  24. }
  25. // NewMathRandomGenerator creates new mathmatical random generator.
  26. // Random generator is seeded by crypto random.
  27. func NewMathRandomGenerator() MathRandomGenerator {
  28. seed, err := CryptoUint64()
  29. if err != nil {
  30. // crypto/rand is unavailable. Fallback to seed by time.
  31. seed = uint64(time.Now().UnixNano())
  32. }
  33. return &mathRandomGenerator{r: mrand.New(mrand.NewSource(int64(seed)))}
  34. }
  35. func (g *mathRandomGenerator) Intn(n int) int {
  36. g.mu.Lock()
  37. v := g.r.Intn(n)
  38. g.mu.Unlock()
  39. return v
  40. }
  41. func (g *mathRandomGenerator) Uint32() uint32 {
  42. g.mu.Lock()
  43. v := g.r.Uint32()
  44. g.mu.Unlock()
  45. return v
  46. }
  47. func (g *mathRandomGenerator) Uint64() uint64 {
  48. g.mu.Lock()
  49. v := g.r.Uint64()
  50. g.mu.Unlock()
  51. return v
  52. }
  53. func (g *mathRandomGenerator) GenerateString(n int, runes string) string {
  54. letters := []rune(runes)
  55. b := make([]rune, n)
  56. for i := range b {
  57. b[i] = letters[g.Intn(len(letters))]
  58. }
  59. return string(b)
  60. }