2017-04-23 20:33:21 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2018-01-26 17:17:38 +08:00
|
|
|
"time"
|
2017-04-23 20:33:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-01-26 17:17:38 +08:00
|
|
|
KC_RAND_KIND_NUM = 0 // 纯数字
|
|
|
|
KC_RAND_KIND_LOWER = 1 // 小写字母
|
|
|
|
KC_RAND_KIND_UPPER = 2 // 大写字母
|
|
|
|
KC_RAND_KIND_ALL = 3 // 数字、大小写字母
|
2017-04-23 20:33:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 随机字符串
|
|
|
|
func Krand(size int, kind int) []byte {
|
|
|
|
ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
|
|
|
|
is_all := kind > 2 || kind < 0
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2018-01-26 17:17:38 +08:00
|
|
|
for i := 0; i < size; i++ {
|
2017-04-23 20:33:21 +08:00
|
|
|
if is_all { // random ikind
|
|
|
|
ikind = rand.Intn(3)
|
|
|
|
}
|
|
|
|
scope, base := kinds[ikind][0], kinds[ikind][1]
|
2018-01-26 17:17:38 +08:00
|
|
|
result[i] = uint8(base + rand.Intn(scope))
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|