广告平台(媒体使用)
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.

3 maanden geleden
3 maanden geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package utils
  2. import (
  3. crand "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "math/rand"
  7. "strconv"
  8. "time"
  9. )
  10. func RandString(l int, c ...string) string {
  11. var (
  12. chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  13. str string
  14. num *big.Int
  15. )
  16. if len(c) > 0 {
  17. chars = c[0]
  18. }
  19. chrLen := int64(len(chars))
  20. for len(str) < l {
  21. num, _ = crand.Int(crand.Reader, big.NewInt(chrLen))
  22. str += string(chars[num.Int64()])
  23. }
  24. return str
  25. }
  26. func RandNum() string {
  27. seed := time.Now().UnixNano() + rand.Int63()
  28. return fmt.Sprintf("%05v", rand.New(rand.NewSource(seed)).Int31n(1000000))
  29. }
  30. func GenerateUniqueRandomNumbers(n int) string {
  31. rand.Seed(time.Now().UnixNano()) // 初始化随机种子
  32. numbers := make([]int, n) // 创建一个切片来保存随机数
  33. for i := range numbers {
  34. j := 0
  35. for {
  36. b := rand.Intn(10) // 生成0-9之间的随机数
  37. numbers[i] = b
  38. for _, num := range numbers[:i] {
  39. if num == b {
  40. j++
  41. break
  42. }
  43. }
  44. if j == 0 {
  45. break
  46. }
  47. }
  48. }
  49. var numbersStr string
  50. for _, v := range numbers {
  51. numbersStr += strconv.Itoa(v)
  52. }
  53. return numbersStr
  54. }