forked from JekaMas/crand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
65 lines (48 loc) · 1019 Bytes
/
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
59
60
61
62
63
64
65
package crand
import (
crand "crypto/rand"
"math"
"math/big"
"math/rand"
)
func NewRand() *Rand {
s := NewSource()
return &Rand{rand.New(s), s}
}
// AddressLength represents Ethereum address length
const AddressLength = 20
func (r *Rand) Address() [AddressLength]byte {
bytes := make([]byte, AddressLength)
_, _ = r.Read(bytes)
var a [AddressLength]byte
copy(a[:], bytes)
return a
}
func BigInt(max *big.Int) *big.Int {
n, _ := crand.Int(crand.Reader, max)
return n
}
// Source doesn't respect a seed
type Source struct{}
func (s Source) Int63() int64 {
intN, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
return intN.Int64()
}
func (s Source) Seed(_ int64) {}
func NewSource() rand.Source {
return Source{}
}
type Source64 struct {
Source
}
func (s Source64) Uint64() uint64 {
n, _ := crand.Int(crand.Reader, big.NewInt(0).SetUint64(math.MaxUint64))
return n.Uint64()
}
func NewSource64() rand.Source64 {
return Source64{}
}
type Rand struct {
*rand.Rand
rand.Source
}