Fix randoms

Use seeded mathematical random where uniqueness is needed
but not required to be cryptographic.
This commit is contained in:
Atsushi Watanabe
2020-07-13 13:48:46 +09:00
parent 00f7ed779a
commit 0157f347e4
8 changed files with 34 additions and 25 deletions

View File

@@ -2,16 +2,17 @@ package signal
import (
"math/rand"
"time"
)
// RandSeq generates a random string to serve as dummy data
//
// It returns a deterministic sequence of values each time a program is run.
// Use rand.Seed() function in your real applications.
func RandSeq(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}