2015-05-19 15:32:40 -04:00
|
|
|
package random
|
|
|
|
|
|
|
|
import (
|
2015-07-29 11:38:09 -04:00
|
|
|
cryptorand "crypto/rand"
|
2015-07-28 20:14:49 -04:00
|
|
|
"io"
|
2015-07-29 11:38:09 -04:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2015-05-19 15:32:40 -04:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-28 20:14:49 -04:00
|
|
|
// Rand is a global *rand.Rand instance, which initilized with NewSource() source.
|
|
|
|
var Rand = rand.New(NewSource())
|
|
|
|
|
|
|
|
// Reader is a global, shared instance of a pseudorandom bytes generator.
|
|
|
|
// It doesn't consume entropy.
|
|
|
|
var Reader io.Reader = &reader{rnd: Rand}
|
|
|
|
|
2015-05-19 15:32:40 -04:00
|
|
|
// copypaste from standard math/rand
|
|
|
|
type lockedSource struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
src rand.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lockedSource) Int63() (n int64) {
|
|
|
|
r.lk.Lock()
|
|
|
|
n = r.src.Int63()
|
|
|
|
r.lk.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lockedSource) Seed(seed int64) {
|
|
|
|
r.lk.Lock()
|
|
|
|
r.src.Seed(seed)
|
|
|
|
r.lk.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSource returns math/rand.Source safe for concurrent use and initialized
|
|
|
|
// with current unix-nano timestamp
|
|
|
|
func NewSource() rand.Source {
|
2015-07-29 11:38:09 -04:00
|
|
|
var seed int64
|
|
|
|
if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
|
|
|
|
// This should not happen, but worst-case fallback to time-based seed.
|
|
|
|
seed = time.Now().UnixNano()
|
|
|
|
} else {
|
|
|
|
seed = cryptoseed.Int64()
|
|
|
|
}
|
2015-05-19 15:32:40 -04:00
|
|
|
return &lockedSource{
|
2015-07-29 11:38:09 -04:00
|
|
|
src: rand.NewSource(seed),
|
2015-05-19 15:32:40 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 20:14:49 -04:00
|
|
|
|
|
|
|
type reader struct {
|
|
|
|
rnd *rand.Rand
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reader) Read(b []byte) (int, error) {
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
val := r.rnd.Int63()
|
|
|
|
for val > 0 {
|
|
|
|
b[i] = byte(val)
|
|
|
|
i++
|
|
|
|
if i == len(b) {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
val >>= 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|