random.go 826 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package random
  2. import (
  3. "math/rand"
  4. )
  5. var (
  6. numSeq [10]rune
  7. lowerSeq [26]rune
  8. upperSeq [26]rune
  9. numLowerSeq [36]rune
  10. numUpperSeq [36]rune
  11. allSeq [62]rune
  12. )
  13. func init() {
  14. for i := 0; i < 10; i++ {
  15. numSeq[i] = rune('0' + i)
  16. }
  17. for i := 0; i < 26; i++ {
  18. lowerSeq[i] = rune('a' + i)
  19. upperSeq[i] = rune('A' + i)
  20. }
  21. copy(numLowerSeq[:], numSeq[:])
  22. copy(numLowerSeq[len(numSeq):], lowerSeq[:])
  23. copy(numUpperSeq[:], numSeq[:])
  24. copy(numUpperSeq[len(numSeq):], upperSeq[:])
  25. copy(allSeq[:], numSeq[:])
  26. copy(allSeq[len(numSeq):], lowerSeq[:])
  27. copy(allSeq[len(numSeq)+len(lowerSeq):], upperSeq[:])
  28. }
  29. func Seq(n int) string {
  30. runes := make([]rune, n)
  31. for i := 0; i < n; i++ {
  32. runes[i] = allSeq[rand.Intn(len(allSeq))]
  33. }
  34. return string(runes)
  35. }
  36. func Num(n int) int {
  37. return rand.Intn(n)
  38. }