-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
50 lines (38 loc) · 756 Bytes
/
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
package gouse
import (
"crypto/rand"
"fmt"
"math/big"
"time"
"github.com/google/uuid"
)
func RandNum(min, max int) int {
// [min, max)
randomInt, err := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
if err != nil {
return 0
}
return int(randomInt.Int64()) + min
}
func RandID() string {
randomID := fmt.Sprint(time.Now().UnixNano())
return randomID
}
func RandStr(n int) string {
var result string
for i := 0; i < n; i++ {
result += string(CHAIN_STR[RandNum(0, len(CHAIN_STR)-1)])
}
return result
}
func RandDigit(n int) string {
var result string
for i := 0; i < n; i++ {
result += string(CHAIN_NUM[RandNum(0, len(CHAIN_NUM)-1)])
}
return result
}
func UUID() string {
uuid := uuid.New()
return uuid.String()
}