Use goroutine-safe version of rand.Source

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2015-05-19 12:32:40 -07:00
parent 31232249f7
commit 66d8f1a5ea
4 changed files with 64 additions and 6 deletions

View file

@ -2,9 +2,10 @@ package stringutils
import (
"bytes"
mathrand "math/rand"
"math/rand"
"strings"
"time"
"github.com/docker/docker/pkg/random"
)
// Generate alpha only random stirng with length n
@ -12,7 +13,7 @@ func GenerateRandomAlphaOnlyString(n int) string {
// make a really long string
letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]byte, n)
r := mathrand.New(mathrand.NewSource(time.Now().UTC().UnixNano()))
r := rand.New(random.NewSource())
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
@ -26,7 +27,7 @@ func GenerateRandomAsciiString(n int) string {
"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
res := make([]byte, n)
for i := 0; i < n; i++ {
res[i] = chars[mathrand.Intn(len(chars))]
res[i] = chars[rand.Intn(len(chars))]
}
return string(res)
}