forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
125 lines (104 loc) · 2.62 KB
/
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
package randomize
import (
"crypto/md5"
"fmt"
"math/rand"
)
const alphabetAll = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const alphabetLowerAlpha = "abcdefghijklmnopqrstuvwxyz"
func randStr(s *Seed, ln int) string {
str := make([]byte, ln)
for i := 0; i < ln; i++ {
str[i] = byte(alphabetAll[s.nextInt()%len(alphabetAll)])
}
return string(str)
}
func randByteSlice(s *Seed, ln int) []byte {
str := make([]byte, ln)
for i := 0; i < ln; i++ {
str[i] = byte(s.nextInt() % 256)
}
return str
}
func randPoint() string {
a := rand.Intn(100)
b := a + 1
return fmt.Sprintf("(%d,%d)", a, b)
}
func randBox() string {
a := rand.Intn(100)
b := a + 1
c := a + 2
d := a + 3
return fmt.Sprintf("(%d,%d),(%d,%d)", a, b, c, d)
}
func randCircle() string {
a, b, c := rand.Intn(100), rand.Intn(100), rand.Intn(100)
return fmt.Sprintf("((%d,%d),%d)", a, b, c)
}
func randNetAddr() string {
return fmt.Sprintf(
"%d.%d.%d.%d",
rand.Intn(254)+1,
rand.Intn(254)+1,
rand.Intn(254)+1,
rand.Intn(254)+1,
)
}
func randMacAddr() string {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
// Set the local bit
buf[0] |= 2
return fmt.Sprintf(
"%02x:%02x:%02x:%02x:%02x:%02x",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
)
}
func randLsn() string {
a := rand.Int63n(9000000)
b := rand.Int63n(9000000)
return fmt.Sprintf("%d/%d", a, b)
}
func randTxID() string {
// Order of integers is relevant
a := rand.Intn(200) + 100
b := a + 100
c := a
d := a + 50
return fmt.Sprintf("%d:%d:%d,%d", a, b, c, d)
}
func randMoney(s *Seed) string {
return fmt.Sprintf("%d.00", s.nextInt())
}
func randTime() string {
return fmt.Sprintf("%d:%d:%d", rand.Intn(24), rand.Intn(60), rand.Intn(60))
}
// StableDBName takes a database name in, and generates
// a random string using the database name as the rand Seed.
// getDBNameHash is used to generate unique test database names.
func StableDBName(input string) string {
return randStrFromSource(stableSource(input), 40)
}
// stableSource takes an input value, and produces a random
// seed from it that will produce very few collisions in
// a 40 character random string made from a different alphabet.
func stableSource(input string) *rand.Rand {
sum := md5.Sum([]byte(input))
var seed int64
for i, byt := range sum {
seed ^= int64(byt) << uint((i*4)%64)
}
return rand.New(rand.NewSource(seed))
}
func randStrFromSource(r *rand.Rand, length int) string {
ln := len(alphabetLowerAlpha)
output := make([]rune, length)
for i := 0; i < length; i++ {
output[i] = rune(alphabetLowerAlpha[r.Intn(ln)])
}
return string(output)
}