-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.random.go
143 lines (116 loc) · 2.98 KB
/
stl.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package stl
import (
"math/rand"
"time"
)
type XPRandomImpl struct{}
type RandomStringOption func(options *RandomStringOptions)
type RandomStringOptions struct {
numberOnly bool
seed string
}
func loadRandomStringOpts(opts []RandomStringOption) *RandomStringOptions {
options := &RandomStringOptions{
numberOnly: false,
seed: "",
}
for _, opt := range opts {
opt(options)
}
return options
}
func (instance *XPRandomImpl) WithNumOnly(numberOnly bool) RandomStringOption {
return func(options *RandomStringOptions) {
options.numberOnly = numberOnly
}
}
func (instance *XPRandomImpl) WithCustomSeed(seed string) RandomStringOption {
return func(options *RandomStringOptions) {
options.seed = seed
}
}
func (instance *XPRandomImpl) RandomString(length int, opts ...RandomStringOption) string {
if length == 0 {
return ""
}
options := loadRandomStringOpts(opts)
var seed = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
if options.numberOnly {
seed = []byte("0123456789")
} else if options.seed != "" {
seed = []byte(options.seed)
}
seedLen := len(seed)
if seedLen < 2 || seedLen > 256 {
panic("Wrong charset length for NewLenChars()")
}
max := 255 - (256 % seedLen)
b := make([]byte, length)
r := make([]byte, length+(length/4))
i := 0
for {
if _, e := rand.Read(r); e != nil {
return ""
}
for _, rb := range r {
c := int(rb)
if c > max {
continue
}
b[i] = seed[c%seedLen]
i++
if i == length {
return string(b)
}
}
}
}
// 获取范围为[0.0, 1.0),类型为float32的随机小数
func (instance *XPRandomImpl) RandomFloat32() float32 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Float32()
}
// 获取范围为[0.0, 1.0),类型为float64的随机小数
func (instance *XPRandomImpl) RandomFloat64() float64 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Float64()
}
func (instance *XPRandomImpl) RandomInt(max int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max)
}
func (instance *XPRandomImpl) RandomIntRange(min, max int) int {
if min >= max || max == 0 {
return max
}
return rand.Intn(max-min) + min
}
func (instance *XPRandomImpl) RandomInt32(max int32) int32 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Int31n(max)
}
func (instance *XPRandomImpl) RandomInt32Range(min, max int32) int32 {
if min >= max || max == 0 {
return max
}
return rand.Int31n(max-min) + min
}
func (instance *XPRandomImpl) RandomInt64(max int64) int64 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Int63n(max)
}
func (instance *XPRandomImpl) RandomInt64Range(min, max int64) int64 {
if min >= max || max == 0 {
return max
}
return rand.Int63n(max-min) + min
}
func (instance *XPRandomImpl) RandomArray(array []interface{}) []interface{} {
for i := len(array) - 1; i >= 0; i-- {
p := instance.RandomInt64Range(0, int64(i))
a := array[i]
array[i] = array[p]
array[p] = a
}
return array
}