-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00_randomizer_for_test.go
82 lines (69 loc) · 1.97 KB
/
00_randomizer_for_test.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
package evendeep
import (
"math/big"
mrand "math/rand"
"sync"
"time"
)
var (
Randtool = &randomizer{}
hundred = big.NewInt(100) //nolint:unused,deadcode,varcheck //test
seededRand = mrand.New(mrand.NewSource(time.Now().UTC().UnixNano())) //nolint:gosec,revive,lll //G404: Use of weak random number generator (math/rand instead of crypto/rand)
mu sync.Mutex
)
// var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
const (
// Alphabets gets the a to z and A to Z
Alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Digits gets 0 to 9
Digits = "0123456789"
// AlphabetNumerics gets Alphabets and Digits
AlphabetNumerics = Alphabets + Digits
// Symbols gets the ascii symbols
Symbols = "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:`"
// ASCII gets the ascii characters
ASCII = AlphabetNumerics + Symbols
)
type randomizer struct {
lastErr error //nolint:unused,structcheck //usable
}
func (r *randomizer) Next() int {
mu.Lock()
defer mu.Unlock()
return seededRand.Int()
}
func (r *randomizer) NextIn(max int) int {
mu.Lock()
defer mu.Unlock()
return seededRand.Intn(max)
}
func (r *randomizer) inRange(min, max int) int {
mu.Lock()
defer mu.Unlock()
return seededRand.Intn(max-min) + min
}
func (r *randomizer) NextInRange(min, max int) int { return r.inRange(min, max) }
func (r *randomizer) NextInt63n(n int64) int64 {
mu.Lock()
defer mu.Unlock()
return seededRand.Int63n(n)
}
func (r *randomizer) NextIntn(n int) int {
mu.Lock()
defer mu.Unlock()
return seededRand.Intn(n)
}
func (r *randomizer) NextFloat64() float64 {
mu.Lock()
defer mu.Unlock()
return seededRand.Float64()
}
// NextStringSimple returns a random string with specified length 'n', just in A..Z
func (r *randomizer) NextStringSimple(n int) string {
b := make([]byte, n)
for i := 0; i < n; i++ {
n := seededRand.Intn(90-65) + 65 //nolint:revive
b[i] = byte(n) // 'a' .. 'z'
}
return string(b)
}