-
Notifications
You must be signed in to change notification settings - Fork 23
/
random.go
49 lines (41 loc) · 1.11 KB
/
random.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
package random
import (
crand "crypto/rand"
"encoding/binary"
"log"
"math/rand"
)
// PseudoGenerator returns *rand.Rand
func PseudoGenerator(seed int64) (g *rand.Rand) {
rnd := rand.New(rand.NewSource(seed))
return rand.New(rand.NewSource(rnd.Int63()))
}
// PseudoGenerators returns list of n *rand.Rand.
// This is needed in cases where random number generators are used in different
// goroutines, so that predictability of the generators can be maintained.
func PseudoGenerators(seed int64, n int) (g []*rand.Rand) {
rnd := rand.New(rand.NewSource(seed))
for i := 0; i < n; i++ {
g = append(g, rand.New(rand.NewSource(rnd.Int63())))
}
return
}
// Int64 returns random int64
func Int64() int64 {
var src CryptoSource
rnd := rand.New(src)
return rnd.Int63()
}
// CryptoSource is used to create random source
type CryptoSource struct{}
func (s CryptoSource) Seed(seed int64) {}
func (s CryptoSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}
func (s CryptoSource) Uint64() (v uint64) {
err := binary.Read(crand.Reader, binary.BigEndian, &v)
if err != nil {
log.Fatal(err)
}
return v
}