-
Notifications
You must be signed in to change notification settings - Fork 0
/
xrand.go
106 lines (92 loc) · 2.26 KB
/
xrand.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
package xrand
var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
digits = []rune("0123456789")
)
// Meet randomly calculate whether the given probability <num>/<total> is met.
func Meet(num, total int) bool {
return Intn(total) < num
}
// MeetProb randomly calculate whether the given probability is met.
func MeetProb(prob float32) bool {
return Intn(1e7) < int(prob*1e7)
}
// N returns a random int between min and max - [min, max].
func N(min, max int) int {
if min >= max {
return min
}
if min >= 0 {
// Because Intn dose not support negative number,
// so we should first shift the value to left,
// then call Intn to produce the random number,
// and finally shift the result to right.
return Intn(max-(min-0)+1) + (min - 0)
}
if min < 0 {
// Because Intn dose not support negative number,
// so we should first shift the value to right,
// then call Intn to produce the random number,
// and finally shift the result to left.
return Intn(max+(0-min)+1) - (0 - min)
}
return 0
}
// Deprecated.
// Alias of N.
func Rand(min, max int) int {
return N(min, max)
}
// Str returns a random string which contains digits and letters, and its length is <n>.
func Str(n int) string {
b := make([]rune, n)
for i := range b {
if Intn(2) == 1 {
b[i] = digits[Intn(10)]
} else {
b[i] = letters[Intn(52)]
}
}
return string(b)
}
// Deprecated.
// Alias of Str.
func RandStr(n int) string {
return Str(n)
}
// Digits returns a random string which contains only digits, and its length is <n>.
func Digits(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = digits[Intn(10)]
}
return string(b)
}
// Deprecated.
// Alias of Digits.
func RandDigits(n int) string {
return Digits(n)
}
// Letters returns a random string which contains only letters, and its length is <n>.
func Letters(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[Intn(52)]
}
return string(b)
}
// Deprecated.
// Alias of Letters.
func RandLetters(n int) string {
return Letters(n)
}
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
func Perm(n int) []int {
m := make([]int, n)
for i := 0; i < n; i++ {
j := Intn(i + 1)
m[i] = m[j]
m[j] = i
}
return m
}