Add random.go
This commit is contained in:
parent
aecf26a45c
commit
b343ad660a
21
random.go
Normal file
21
random.go
Normal 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()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user