locked_source.go 538 B

123456789101112131415161718192021222324252627282930
  1. package sdkrand
  2. import (
  3. "math/rand"
  4. "sync"
  5. "time"
  6. )
  7. // lockedSource is a thread-safe implementation of rand.Source
  8. type lockedSource struct {
  9. lk sync.Mutex
  10. src rand.Source
  11. }
  12. func (r *lockedSource) Int63() (n int64) {
  13. r.lk.Lock()
  14. n = r.src.Int63()
  15. r.lk.Unlock()
  16. return
  17. }
  18. func (r *lockedSource) Seed(seed int64) {
  19. r.lk.Lock()
  20. r.src.Seed(seed)
  21. r.lk.Unlock()
  22. }
  23. // SeededRand is a new RNG using a thread safe implementation of rand.Source
  24. var SeededRand = rand.New(&lockedSource{src: rand.NewSource(time.Now().UnixNano())})