github.com/jogo/docker@v1.7.0-rc1/pkg/random/random.go (about) 1 package random 2 3 import ( 4 "math/rand" 5 "sync" 6 "time" 7 ) 8 9 // copypaste from standard math/rand 10 type lockedSource struct { 11 lk sync.Mutex 12 src rand.Source 13 } 14 15 func (r *lockedSource) Int63() (n int64) { 16 r.lk.Lock() 17 n = r.src.Int63() 18 r.lk.Unlock() 19 return 20 } 21 22 func (r *lockedSource) Seed(seed int64) { 23 r.lk.Lock() 24 r.src.Seed(seed) 25 r.lk.Unlock() 26 } 27 28 // NewSource returns math/rand.Source safe for concurrent use and initialized 29 // with current unix-nano timestamp 30 func NewSource() rand.Source { 31 return &lockedSource{ 32 src: rand.NewSource(time.Now().UnixNano()), 33 } 34 }