Commit cc7a869e authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

threadsafe SeededRand

parent 6e9cb06b
package testutil
import (
"math/rand"
"sync"
"time"
)
var SeededRand *rand.Rand
func init() {
SeededRand = NewSeededRand(time.Now().UTC().UnixNano())
}
func NewSeededRand(seed int64) *rand.Rand {
src := rand.NewSource(seed)
return rand.New(&LockedRandSource{src: src})
}
type LockedRandSource struct {
lk sync.Mutex
src rand.Source
}
func (r *LockedRandSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}
func (r *LockedRandSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment