-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
51 lines (42 loc) · 954 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
50
51
package util
import (
"math/rand"
"strings"
)
func _alphabet() (ret string) {
for i := 'a'; i <= 'z'; i++ {
ret += string(i)
}
ret += strings.ToUpper(ret)
return
}
var numeric = "0123456789"
var alphabets = _alphabet()
var alphanumeric = alphabets + numeric
func RandomIntBetween(min, max int) int {
return rand.Intn(max-min) + min
}
func RandomString(low int, high int) (ret string) {
return RandomAlphabets(RandomIntBetween(low, high), true)
}
func RandomInt(low int, high int) int {
return rand.Intn(high-low) + low
}
func RandomFloat64(low float64, high float64) float64 {
return rand.Float64()*(high-low) + low
}
func RandomBool() bool {
return rand.Intn(2) == 1
}
func RandomAlphabets(l int, lowerCase bool) string {
b := strings.Builder{}
al := len(alphabets)
r := NewRand()
for i := 0; i < l; i++ {
_ = b.WriteByte(alphabets[r.Intn(al)])
}
if lowerCase {
return strings.ToLower(b.String())
}
return b.String()
}