Milen/util/rand.go

41 lines
729 B
Go

package util
import (
crand "crypto/rand"
mrand "math/rand"
)
type src struct{}
func (s src) Int63() int64 {
result := make([]byte, 8)
_, err := crand.Read(result)
if err != nil {
panic(err)
}
return int64(result[0]&0x7F)<<56 |
int64(result[1])<<48 |
int64(result[2])<<40 |
int64(result[3])<<32 |
int64(result[4])<<24 |
int64(result[5])<<16 |
int64(result[6])<<8 |
int64(result[7])<<0
}
func (s src) Seed(seed int64) {
// We don't do seeds here.
}
var Rand = mrand.New(src{})
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+="
func RandomBase64(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[Rand.Intn(64)]
}
return string(b)
}