/
random.go
51 lines (44 loc) · 1.04 KB
/
random.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Package random provides a concurrency safe
// https://golang.org/pkg/math/rand/#Rand object.
package random
import (
"math/rand"
"sync"
)
// NewRand is a convenience function to generate a new
// https://golang.org/pkg/math/rand/#Rand object that is concurrency safe.
func NewRand(seed int64) *rand.Rand {
source := NewConcurrencySafeSource(seed)
random := rand.New(source)
return random
}
// NewConcurrencySafeSource creates a concurrency safe source.
func NewConcurrencySafeSource(seed int64) rand.Source {
source := rand.NewSource(seed)
source64 := source.(rand.Source64)
return &concurrenySafeSource{
src: source64,
mut: &sync.Mutex{},
}
}
type concurrenySafeSource struct {
src rand.Source64
mut *sync.Mutex
}
func (c *concurrenySafeSource) Int63() int64 {
c.mut.Lock()
res := c.src.Int63()
c.mut.Unlock()
return res
}
func (c *concurrenySafeSource) Uint64() uint64 {
c.mut.Lock()
res := c.src.Uint64()
c.mut.Unlock()
return res
}
func (c *concurrenySafeSource) Seed(seed int64) {
c.mut.Lock()
c.src.Seed(seed)
c.mut.Unlock()
}