var alphanum = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
func randomString(n int, rng *rand.Rand) string {
a := make([]rune, n)
for i := range a {
a[i] = alphanum[rng.Intn(len(alphanum))]
}
return string(a)
}
This version is slightly more generic: it works with an alphabet of arbitrary runes, which don't need to fit in a single byte each.
Note that the package math/rand is not crypto-secure.
Note that the package math/rand is not crypto-secure.
const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
func randomString(n int, rng *rand.Rand) string {
a := make([]byte, n)
for i := range a {
a[i] = alphanum[rng.Intn(len(alphanum))]
}
return string(a)
}
Using a custom rand.Rand instance lets you control the seed, and is also better for performance (lock-free).
Note that the package math/rand is not crypto-secure.
Note that the package math/rand is not crypto-secure.
const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
func randomString(n int) string {
a := make([]byte, n)
for i := range a {
a[i] = alphanum[rand.Intn(len(alphanum))]
}
return string(a)
}
Each of these runes fits in a single byte.
The default RNG can be run concurrently, as it incurs the cost of a Mutex.
Note that the package math/rand is not crypto-secure
The default RNG can be run concurrently, as it incurs the cost of a Mutex.
Note that the package math/rand is not crypto-secure