-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
58 lines (48 loc) · 1.41 KB
/
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
52
53
54
55
56
57
58
/* Copyright 2015 Ron Zhang <ronzxy@mx.aketi.cn>. All rights reserved.
*
* Licensed under the Apache License, version 2.0 (the "License").
* You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package helper
import (
"fmt"
"math/rand"
"strconv"
"time"
)
var Rand = NewRandHelper()
type RandHelper struct{}
func NewRandHelper() *RandHelper {
return &RandHelper{}
}
func (this *RandHelper) Num(length int) int64 {
var s string
r := rand.New(rand.NewSource(time.Now().UnixNano()))
p := r.Perm(length)
for i := 0; i < length; i++ {
s = fmt.Sprintf("%s%d", s, p[i])
}
num, _ := strconv.ParseInt(s, 10, 64)
return num
}
//生成随机字符串
func (this *RandHelper) Bytes(length int) []byte {
buf := make([]byte, 0)
str := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+=/")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
buf = append(buf, str[r.Intn(len(str))])
}
return buf
}
//生成随机字符串
func (this *RandHelper) String(length int) string {
buf := this.Bytes(length)
return string(buf)
}