1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 06:41:34 +00:00
Beego/utils/rand.go

21 lines
474 B
Go
Raw Normal View History

2014-01-16 12:53:35 +00:00
package utils
import (
"crypto/rand"
)
// RandomCreateBytes generate random []byte by specify chars.
func RandomCreateBytes(n int, alphabets ...byte) []byte {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
if len(alphabets) == 0 {
bytes[i] = alphanum[b%byte(len(alphanum))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
return bytes
}