-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrand.go
49 lines (42 loc) · 861 Bytes
/
rand.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
// Fast Rand
// On-start generated random values used to speed up performance
// of rand() instead of using math.rand().
// Only used when performance is critical.
package main
import (
"math/rand"
"time"
)
type fRand struct {
randInt []int
randFloat64 []float64
rintc int
rfloatc int
max int
}
func (r *fRand) create(max int) {
r.rintc = -1
r.rfloatc = -1
r.max = max
r.randInt = make([]int, max)
r.randFloat64 = make([]float64, max)
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < max; i++ {
r.randInt[i] = rand.Intn(10)
r.randFloat64[i] = rand.Float64()
}
}
func (r *fRand) rand() int {
if r.rintc >= r.max-1 {
r.rintc = -1
}
r.rintc++
return r.randInt[r.rintc]
}
func (r *fRand) randFloat() float64 {
if r.rfloatc >= r.max-1 {
r.rfloatc = -1
}
r.rfloatc++
return r.randFloat64[r.rfloatc]
}