diff --git a/random/random.go b/random/random.go index 05b7f7f..865f5f3 100644 --- a/random/random.go +++ b/random/random.go @@ -1,11 +1,19 @@ package random import ( + "io" "math/rand" "sync" "time" ) +// 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} + // copypaste from standard math/rand type lockedSource struct { lk sync.Mutex @@ -32,3 +40,22 @@ func NewSource() rand.Source { src: rand.NewSource(time.Now().UnixNano()), } } + +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 + } + } +}