-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
52 lines (45 loc) · 1.07 KB
/
int.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
package numeric
import (
"bytes"
"crypto/rand"
"encoding/binary"
"io"
)
var (
rander = rand.Reader // random function
r = make([]byte, 8)
)
// Int64 creates a random 64 bit integer using crypto.rand
func Int64() (i int64) {
randomBits(r)
buf := bytes.NewBuffer(r)
binary.Read(buf, binary.LittleEndian, &i)
return i
}
// UInt64 creates a random 64 bit unsigned integer using crypto.rand
func UInt64() (i uint64) {
randomBits(r)
buf := bytes.NewBuffer(r)
binary.Read(buf, binary.LittleEndian, &i)
return i
}
// Int32 creates a random 32 bit integer using crypto.rand
func Int32() (i int32) {
randomBits(r)
buf := bytes.NewBuffer(r)
binary.Read(buf, binary.LittleEndian, &i)
return i
}
// UInt32 creates a random 32 bit unsigned integer using crypto.rand
func UInt32() (i uint32) {
randomBits(r)
buf := bytes.NewBuffer(r)
binary.Read(buf, binary.LittleEndian, &i)
return i
}
// randomBits completely fills slice b with random data.
func randomBits(b []byte) {
if _, err := io.ReadFull(rander, b); err != nil {
panic(err.Error()) // rand should never fail
}
}