random.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package stringx
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "time"
  8. )
  9. const (
  10. letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  11. letterIdxBits = 6 // 6 bits to represent a letter index
  12. idLen = 8
  13. defaultRandLen = 8
  14. letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
  15. letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
  16. )
  17. var src = newLockedSource(time.Now().UnixNano())
  18. type lockedSource struct {
  19. source rand.Source
  20. lock sync.Mutex
  21. }
  22. func newLockedSource(seed int64) *lockedSource {
  23. return &lockedSource{
  24. source: rand.NewSource(seed),
  25. }
  26. }
  27. func (ls *lockedSource) Int63() int64 {
  28. ls.lock.Lock()
  29. defer ls.lock.Unlock()
  30. return ls.source.Int63()
  31. }
  32. func (ls *lockedSource) Seed(seed int64) {
  33. ls.lock.Lock()
  34. defer ls.lock.Unlock()
  35. ls.source.Seed(seed)
  36. }
  37. // Rand returns a random string.
  38. func Rand() string {
  39. return Randn(defaultRandLen)
  40. }
  41. // RandId returns a random id string.
  42. func RandId() string {
  43. b := make([]byte, idLen)
  44. _, err := crand.Read(b)
  45. if err != nil {
  46. return Randn(idLen)
  47. }
  48. return fmt.Sprintf("%x%x%x%x", b[0:2], b[2:4], b[4:6], b[6:8])
  49. }
  50. // Randn returns a random string with length n.
  51. func Randn(n int) string {
  52. b := make([]byte, n)
  53. // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
  54. for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
  55. if remain == 0 {
  56. cache, remain = src.Int63(), letterIdxMax
  57. }
  58. if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
  59. b[i] = letterBytes[idx]
  60. i--
  61. }
  62. cache >>= letterIdxBits
  63. remain--
  64. }
  65. return string(b)
  66. }
  67. // Seed sets the seed to seed.
  68. func Seed(seed int64) {
  69. src.Seed(seed)
  70. }