github.com/hedzr/evendeep@v0.4.8/00_randomizer_for_test.go (about) 1 package evendeep 2 3 import ( 4 "math/big" 5 mrand "math/rand" 6 "sync" 7 "time" 8 ) 9 10 var ( 11 Randtool = &randomizer{} 12 13 hundred = big.NewInt(100) //nolint:unused,deadcode,varcheck //test 14 seededRand = mrand.New(mrand.NewSource(time.Now().UTC().UnixNano())) //nolint:gosec //G404: Use of weak random number generator (math/rand instead of crypto/rand) 15 mu sync.Mutex 16 ) 17 18 // var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 19 const ( 20 // Alphabets gets the a to z and A to Z 21 Alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 22 // Digits gets 0 to 9 23 Digits = "0123456789" 24 // AlphabetNumerics gets Alphabets and Digits 25 AlphabetNumerics = Alphabets + Digits 26 // Symbols gets the ascii symbols 27 Symbols = "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:`" 28 // ASCII gets the ascii characters 29 ASCII = AlphabetNumerics + Symbols 30 ) 31 32 type randomizer struct { 33 lastErr error //nolint:unused,structcheck //usable 34 } 35 36 func (r *randomizer) Next() int { 37 mu.Lock() 38 defer mu.Unlock() 39 return seededRand.Int() 40 } 41 42 func (r *randomizer) NextIn(max int) int { 43 mu.Lock() 44 defer mu.Unlock() 45 return seededRand.Intn(max) 46 } 47 48 func (r *randomizer) inRange(min, max int) int { 49 mu.Lock() 50 defer mu.Unlock() 51 return seededRand.Intn(max-min) + min 52 } 53 54 func (r *randomizer) NextInRange(min, max int) int { return r.inRange(min, max) } 55 56 func (r *randomizer) NextInt63n(n int64) int64 { 57 mu.Lock() 58 defer mu.Unlock() 59 return seededRand.Int63n(n) 60 } 61 62 func (r *randomizer) NextIntn(n int) int { 63 mu.Lock() 64 defer mu.Unlock() 65 return seededRand.Intn(n) 66 } 67 68 func (r *randomizer) NextFloat64() float64 { 69 mu.Lock() 70 defer mu.Unlock() 71 return seededRand.Float64() 72 } 73 74 // NextStringSimple returns a random string with specified length 'n', just in A..Z 75 func (r *randomizer) NextStringSimple(n int) string { 76 b := make([]byte, n) 77 for i := 0; i < n; i++ { 78 n := seededRand.Intn(90-65) + 65 79 b[i] = byte(n) // 'a' .. 'z' 80 } 81 return string(b) 82 }