rand.go 404B

12345678910111213141516171819202122232425262728
  1. package common
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "strings"
  6. "time"
  7. )
  8. const (
  9. numLen = 10 //随机序列长度
  10. )
  11. var (
  12. numeric = [numLen]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} //随机序列
  13. )
  14. func GetRand(width int) string {
  15. r := len(numeric)
  16. rand.Seed(time.Now().UnixNano())
  17. var sb strings.Builder
  18. for i := 0; i < width; i++ {
  19. fmt.Fprintf(&sb, "%d", numeric[rand.Intn(r)])
  20. }
  21. return sb.String()
  22. }