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

преди 4 месеца
преди 4 месеца
преди 4 месеца
преди 4 месеца
преди 3 месеца
преди 4 месеца
преди 3 месеца
преди 4 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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() + rand.Int63()) // 初始化随机种子
  32. numbers := make([]int, 0, n) // 创建一个切片来保存随机数,初始容量为n
  33. used := make(map[int]bool) // 跟踪已使用的数字
  34. for len(numbers) < n {
  35. b := rand.Intn(10) // 生成0-9之间的随机数
  36. if !used[b] {
  37. numbers = append(numbers, b)
  38. used[b] = true
  39. }
  40. }
  41. var numbersStr string
  42. for _, v := range numbers {
  43. numbersStr += strconv.Itoa(v)
  44. }
  45. return numbersStr
  46. }