蛋蛋星球 后台端
 
 
 
 

32 righe
591 B

  1. package utils
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "math/rand"
  7. "time"
  8. )
  9. func RandString(l int, c ...string) string {
  10. var (
  11. chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  12. str string
  13. num *big.Int
  14. )
  15. if len(c) > 0 {
  16. chars = c[0]
  17. }
  18. chrLen := int64(len(chars))
  19. for len(str) < l {
  20. num, _ = crand.Int(crand.Reader, big.NewInt(chrLen))
  21. str += string(chars[num.Int64()])
  22. }
  23. return str
  24. }
  25. func RandNum() string {
  26. seed := time.Now().UnixNano() + rand.Int63()
  27. return fmt.Sprintf("%05v", rand.New(rand.NewSource(seed)).Int31n(1000000))
  28. }