-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.go
105 lines (87 loc) · 1.75 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package u
import (
"math/rand"
"sync"
"time"
)
var GlobalRand1 = NewRand(rand.NewSource(int64(time.Now().Nanosecond())))
var GlobalRand2 = NewRand(rand.NewSource(int64(time.Now().Nanosecond())))
func NewRand(source rand.Source) *Rand {
return &Rand{
goRand: rand.New(source),
}
}
type Rand struct {
goRand *rand.Rand
lock sync.Mutex
}
func (r *Rand) Seed(seed int64) {
r.lock.Lock()
defer r.lock.Unlock()
r.goRand.Seed(seed)
}
func (r *Rand) Int63() int64 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Int63()
}
func (r *Rand) Uint32() uint32 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Uint32()
}
func (r *Rand) Uint64() uint64 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Uint64()
}
func (r *Rand) Int31() int32 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Int31()
}
func (r *Rand) Int() int {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Int()
}
func (r *Rand) Int63n(n int64) int64 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Int63n(n)
}
func (r *Rand) Int31n(n int32) int32 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Int31n(n)
}
func (r *Rand) Intn(n int) int {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Intn(n)
}
func (r *Rand) Float64() float64 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Float64()
}
func (r *Rand) Float32() float32 {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Float32()
}
func (r *Rand) Perm(n int) []int {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Perm(n)
}
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
r.lock.Lock()
defer r.lock.Unlock()
r.goRand.Shuffle(n, swap)
}
func (r *Rand) Read(p []byte) (n int, err error) {
r.lock.Lock()
defer r.lock.Unlock()
return r.goRand.Read(p)
}