-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.go
49 lines (39 loc) · 858 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
// rand.go -- utilities that generate random values
//
// (c) Sudhi Herle 2018
//
// Author: Sudhi Herle <sudhi@herle.net>
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package chd
import (
"crypto/rand"
"encoding/binary"
"io"
)
func randbytes(n int) []byte {
b := make([]byte, n)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
panic("can't read crypto/rand")
}
return b
}
func rand32() uint32 {
var b [4]byte
_, err := io.ReadFull(rand.Reader, b[:])
if err != nil {
panic("can't read crypto/rand")
}
return binary.BigEndian.Uint32(b[:])
}
func rand64() uint64 {
var b [8]byte
_, err := io.ReadFull(rand.Reader, b[:])
if err != nil {
panic("can't read crypto/rand")
}
return binary.BigEndian.Uint64(b[:])
}