🐛 set random seed

This commit is contained in:
微凉
2022-02-04 16:08:15 +08:00
parent 86cda58b22
commit a22903533e
3 changed files with 14 additions and 7 deletions

View File

@ -3,13 +3,22 @@ package utils
import (
"math/rand"
"strings"
"time"
)
var Rand *rand.Rand
func init() {
s := rand.NewSource(time.Now().UnixNano())
Rand = rand.New(s)
}
func RandomStr(n int) string {
builder := strings.Builder{}
t := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i := 0; i < n; i++ {
r := rand.Intn(len(t))
r := Rand.Intn(len(t))
builder.WriteString(t[r : r+1])
}
return builder.String()