-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
68 lines (53 loc) · 1.1 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
package rand
import (
"crypto/rand"
"math/big"
"github.com/pkg/errors"
)
// Random provider.
type Random struct {
reader Reader
}
// Int64 returns random int64.
func (r *Random) Int64(max int64, min ...int64) (int64, error) {
var from int64
if len(min) > 0 {
from = min[0]
}
if from == max {
return max, nil
}
if from > max {
return 0, errors.WithStack(Error{
"max should be greater than min",
})
}
n, err := rand.Int(r.reader, big.NewInt(max-from))
if err != nil {
return 0, errors.WithStack(err)
}
return from + n.Int64(), nil
}
// String returns random stwring.
func (r *Random) String(alphabet Alphabet, max int64, min ...int64) (string, error) {
if alphabet == "" {
return "", errors.WithStack(Error{
"alphabet shouldn't be empty string",
})
}
length, err := r.Int64(max, min...)
if err != nil {
return "", errors.WithStack(err)
}
runes := []rune(alphabet)
l := len(runes)
res := make([]rune, length)
for i := int64(0); i < length; i++ {
n, err := r.Int64(int64(l))
if err != nil {
return "", err
}
res[i] = runes[int(n)]
}
return string(res), nil
}