您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. }
  29. //x的y次方
  30. func RandPow(l int) string {
  31. var i = "1"
  32. for j := 0; j < l; j++ {
  33. i += "0"
  34. }
  35. k := StrToInt64(i)
  36. n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
  37. ls := "%0" + IntToStr(l) + "v"
  38. str := fmt.Sprintf(ls, n)
  39. //min := int(math.Pow10(l - 1))
  40. //max := int(math.Pow10(l) - 1)
  41. return str
  42. }