Add random.go

This commit is contained in:
殷勇 2023-08-18 11:38:43 +08:00
parent aecf26a45c
commit b343ad660a

21
random.go Normal file
View File

@ -0,0 +1,21 @@
package q5
import (
"math/rand"
"strings"
)
// RandomString generates a random string of length n
const alphabet = "abcdefghijklmnopqrstuvwxyz"
func RandomString(n int) string {
var sb strings.Builder
k := len(alphabet)
for i := 0; i < n; i++ {
c := alphabet[rand.Intn(k)]
sb.WriteByte(c)
}
return sb.String()
}