附近小店
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.

shuffle.go 838 B

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. // 打乱随机字符串
  7. func ShuffleString(s *string) {
  8. if len(*s) > 1 {
  9. b := []byte(*s)
  10. rand.Seed(time.Now().UnixNano())
  11. rand.Shuffle(len(b), func(x, y int) {
  12. b[x], b[y] = b[y], b[x]
  13. })
  14. *s = string(b)
  15. }
  16. }
  17. // 打乱随机slice
  18. func ShuffleSliceBytes(b []byte) {
  19. if len(b) > 1 {
  20. rand.Seed(time.Now().UnixNano())
  21. rand.Shuffle(len(b), func(x, y int) {
  22. b[x], b[y] = b[y], b[x]
  23. })
  24. }
  25. }
  26. // 打乱slice int
  27. func ShuffleSliceInt(i []int) {
  28. if len(i) > 1 {
  29. rand.Seed(time.Now().UnixNano())
  30. rand.Shuffle(len(i), func(x, y int) {
  31. i[x], i[y] = i[y], i[x]
  32. })
  33. }
  34. }
  35. // 打乱slice interface
  36. func ShuffleSliceInterface(i []interface{}) {
  37. if len(i) > 1 {
  38. rand.Seed(time.Now().UnixNano())
  39. rand.Shuffle(len(i), func(x, y int) {
  40. i[x], i[y] = i[y], i[x]
  41. })
  42. }
  43. }