libString.go 607 B

1234567891011121314151617181920212223242526272829
  1. package lib
  2. import (
  3. "math/rand"
  4. "strings"
  5. "time"
  6. )
  7. //#取得随机字符串:通过打乱slice来操作
  8. func GetRandstring(length int,char string,rand_x int64) string {
  9. if length < 1 {
  10. return ""
  11. }
  12. if len(char) <= 6 || len(char) <= length{
  13. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  14. }
  15. charArr := strings.Split(char, "")
  16. ran := rand.New(rand.NewSource(time.Now().Unix()+rand_x))
  17. l := len(charArr)
  18. for i := l - 1; i > 0; i-- {
  19. r := ran.Intn(i)
  20. charArr[r], charArr[i] = charArr[i], charArr[r]
  21. }
  22. rchar := charArr[:length]
  23. return strings.Join(rchar, "")
  24. }