random: optimize allocation & simplify loop

This commit is contained in:
Adrien Delorme 2018-09-05 10:27:02 +02:00
parent 4682b3a9d2
commit 5a8af5500b
1 changed files with 4 additions and 3 deletions

View File

@ -25,8 +25,9 @@ func AlphaNumUpper(length int) string { return String(PossibleAlphaNumUpper, len
func String(chooseFrom string, length int) (randomString string) {
cflen := len(chooseFrom)
for i := 0; i < length; i++ {
randomString += string(chooseFrom[rnd.Intn(cflen)])
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = chooseFrom[rnd.Intn(cflen)]
}
return
return string(bytes)
}