randomId.go 867 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package unity
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. // RandomId 生成32位随机id
  7. func RandomId() string {
  8. const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. rand.Seed(time.Now().UnixNano())
  10. b := make([]byte, 8)
  11. for i := range b {
  12. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  13. }
  14. return string(b)
  15. }
  16. func RandomIds(lenght int) string {
  17. const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  18. rand.Seed(time.Now().UnixNano())
  19. b := make([]byte, lenght)
  20. for i := range b {
  21. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  22. }
  23. return string(b)
  24. }
  25. // RandomAppID 生成12位随机appid
  26. func RandomAppID() string {
  27. const letterBytes = "0123456789"
  28. rand.Seed(time.Now().UnixNano())
  29. b := make([]byte, 12)
  30. for i := range b {
  31. b[i] = letterBytes[rand.Intn(len(letterBytes))]
  32. }
  33. return string(b)
  34. }