golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

47 regels
911 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. }
  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. }