string.go 974 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package gofakeit
  2. import (
  3. "math/rand"
  4. )
  5. // Letter will generate a single random lower case ASCII letter
  6. func Letter() string {
  7. return string(randLetter())
  8. }
  9. // Digit will generate a single ASCII digit
  10. func Digit() string {
  11. return string(randDigit())
  12. }
  13. // Lexify will replace ? will random generated letters
  14. func Lexify(str string) string {
  15. return replaceWithLetters(str)
  16. }
  17. // ShuffleStrings will randomize a slice of strings
  18. func ShuffleStrings(a []string) {
  19. swap := func(i, j int) {
  20. a[i], a[j] = a[j], a[i]
  21. }
  22. //to avoid upgrading to 1.10 I copied the algorithm
  23. n := len(a)
  24. if n <= 1 {
  25. return
  26. }
  27. //if size is > int32 probably it will never finish, or ran out of entropy
  28. i := n - 1
  29. for ; i > 0; i-- {
  30. j := int(rand.Int31n(int32(i + 1)))
  31. swap(i, j)
  32. }
  33. }
  34. // RandString will take in a slice of string and return a randomly selected value
  35. func RandString(a []string) string {
  36. size := len(a)
  37. if size == 0 {
  38. return ""
  39. }
  40. return a[rand.Intn(size)]
  41. }