-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
49 lines (38 loc) · 797 Bytes
/
base.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
package easyrand
import (
"crypto/rand"
"math/big"
)
func randomInt64(mn, mx int64) (int64, error) {
x := big.NewInt(mx - mn)
n, err := rand.Int(rand.Reader, x)
if err != nil {
return 0, err
}
return mn + n.Int64(), nil
}
func randomUInt64(mn, mx uint64) (uint64, error) {
x := new(big.Int).SetUint64(mx - mn)
n, err := rand.Int(rand.Reader, x)
if err != nil {
return 0, err
}
return mn + n.Uint64(), nil
}
func randomBytes(n int) ([]byte, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return nil, err
}
return b, nil
}
func randomBytesAlphabet(alphabet []byte, n int) ([]byte, error) {
b, err := randomBytes(n)
if err != nil {
return nil, err
}
for i := 0; i < n; i++ {
b[i] = alphabet[int(b[i])%len(alphabet)]
}
return b, nil
}