forked from iotaledger/hive.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomness.go
30 lines (23 loc) · 827 Bytes
/
randomness.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
package crypto
import (
crand "crypto/rand"
"encoding/binary"
"math/rand"
)
// RandomnessSource implements a cryptographically secure randomness source.
type RandomnessSource struct{}
// Seed is usually used to initialize the generator to a deterministic state but we want real randomness.
func (s RandomnessSource) Seed(int64) {}
// Int63 returns a non-negative random 63-bit integer as an int64.
func (s RandomnessSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}
// Uint64 returns a random 64-bit value as a uint64.
func (s RandomnessSource) Uint64() (v uint64) {
if err := binary.Read(crand.Reader, binary.BigEndian, &v); err != nil {
panic(err)
}
return v
}
// Randomness contains an Rand instance of the previously defined RandomnessSource.
var Randomness = rand.New(RandomnessSource{})