summaryrefslogtreecommitdiff
path: root/cmd/ponzu/vendor/github.com/nilslice/rand/rand.go
blob: cbff54afc588a84476862d1fec0dc6e6c50d1973 (plain)
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
// rand provides a convenient package to use crypto/rand as default
// but falls back to math/rand if necessary
package rand

import (
	crand "crypto/rand"
	mrand "math/rand"
	"time"
)

var g_mathRand = mrand.New(mrand.NewSource(time.Now().Unix()))

// Read fills the slice with random bytes
func Read(xs []byte) {
	length := len(xs)
	n, err := crand.Read(xs)
	if n != length || err != nil {
		for length > 0 {
			length--
			xs[length] = byte(g_mathRand.Int31n(256))
		}
	}
}

// Int63 returns a non-negative 63-bit integer as an int64
func Int63() int64 {
	xs := make([]byte, 8)
	var n int64

	Read(xs)
	xs[0] &= 0x7F
	for _, x := range xs {
		n <<= 4
		n |= int64(x)
	}

	return n
}