You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package rand
import (
"math/rand"
"strconv"
"time"
)
// 包含大小写和数字
func (r *Rand) RandomString(n int) string {
letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
//var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
result := make([]byte, n)
rand.Seed(time.Now().Unix())
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
}
return string(result)
}
// 生成指定位数的数字字符串第一位不为0
func (r *Rand) RandomCode(n int) string {
numbers := []byte("0123456789")
numbers_no_zero := []byte("123456789")
result := make([]byte, n)
rand.Seed(time.Now().Unix())
//code := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
for i := range result {
result[i] = numbers[rand.Intn(len(numbers))]
}
//阿里大于验证码短信首字母不能为0
if result[0] == 0 {
{
result[0] = numbers_no_zero[rand.Intn(len(numbers_no_zero))]
}
}
return string(result)
}
// 6位随机验证码
func (r *Rand) GetNum6() string {
//deltaNumber := exponent(10,6)
//baseNumber := exponent(10,7)
rand.Seed(time.Now().UnixNano())
code := rand.Intn(899999) + 100000
//code := rand.Intn(baseNumber-baseNumber-1) + deltaNumber
res := strconv.Itoa(code) //转字符串返回
return res
}