-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoshiro256.go
95 lines (73 loc) · 2.16 KB
/
xoshiro256.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package prng
import (
"math/rand"
"github.com/ericyan/prng/internal/splitmix64"
)
// A xoshiro256Plus is an instance of xoshiro256+ generator.
type xoshiro256Plus [4]uint64
// NewXoroshiro128Plus returns a new xoshiro256+ generator seeded with
// the given value.
func NewXoshiro256Plus(seed int64) rand.Source64 {
s := new(xoshiro256Plus)
s.Seed(seed)
return s
}
// Seed initializes the generator to a deterministic state.
//
// It first seeds a SplitMix64 generator with the given 64-bit value and
// then uses the outputs to seed the xoshiro256+ generator which has 256
// bits of state.
func (s *xoshiro256Plus) Seed(seed int64) {
r := splitmix64.New(seed)
s[0], s[1], s[2], s[3] = r.Uint64(), r.Uint64(), r.Uint64(), r.Uint64()
}
// Uint64 returns a pseudo-random 64-bit value as a uint64.
func (s *xoshiro256Plus) Uint64() uint64 {
result := s[0] + s[3]
t := s[1] << 17
s[2] ^= s[0]
s[3] ^= s[1]
s[1] ^= s[2]
s[0] ^= s[3]
s[2] ^= t
s[3] = rotl(s[3], 45)
return result
}
// Int63 returns a non-negative pseudorandom 63-bit integer as an int64.
func (s *xoshiro256Plus) Int63() int64 {
return int63(s.Uint64())
}
// A xoshiro256StarStar is an instance of xoshiro256** generator.
type xoshiro256StarStar [4]uint64
// NewXoroshiro128StarStar returns a new xoshiro256** generator seeded
// with the given value.
func NewXoshiro256StarStar(seed int64) rand.Source64 {
s := new(xoshiro256StarStar)
s.Seed(seed)
return s
}
// Seed initializes the generator to a deterministic state.
//
// It first seeds a SplitMix64 generator with the given 64-bit value and
// then uses the outputs to seed the xoshiro256** generator which has
// 256 bits of state.
func (s *xoshiro256StarStar) Seed(seed int64) {
r := splitmix64.New(seed)
s[0], s[1] = r.Uint64(), r.Uint64()
}
// Uint64 returns a pseudo-random 64-bit value as a uint64.
func (s *xoshiro256StarStar) Uint64() uint64 {
result := rotl(s[1]*5, 7) * 9
t := s[1] << 17
s[2] ^= s[0]
s[3] ^= s[1]
s[1] ^= s[2]
s[0] ^= s[3]
s[2] ^= t
s[3] = rotl(s[3], 45)
return result
}
// Int63 returns a non-negative pseudorandom 63-bit integer as an int64.
func (s *xoshiro256StarStar) Int63() int64 {
return int63(s.Uint64())
}