-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmt19937.go
190 lines (178 loc) · 5.17 KB
/
mt19937.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// mt19937.go - an implementation of the 64bit Mersenne Twister PRNG
// Copyright (C) 2013 Jochen Voss <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mt19937
const (
n = 312
m = 156
notSeeded = n + 1
hiMask uint64 = 0xffffffff80000000
loMask uint64 = 0x000000007fffffff
matrixA uint64 = 0xB5026F5AA96619E9
)
// MT19937 is the structure to hold the state of one instance of the
// Mersenne Twister PRNG. New instances can be allocated using the
// mt19937.New() function. MT19937 implements the rand.Source
// interface and rand.New() from the math/rand package can be used to
// generate different distributions from a MT19937 PRNG.
//
// This class is not safe for concurrent accesss by different
// goroutines. If more than one goroutine accesses the PRNG, the
// callers must synchronise access using sync.Mutex or similar.
type MT19937 struct {
state []uint64
index int
}
// New allocates a new instance of the 64bit Mersenne Twister.
// A seed can be set using the .Seed() or .SeedFromSlice() methods.
// If no seed is set explicitly, a default seed is used instead.
func New() *MT19937 {
res := &MT19937{
state: make([]uint64, n),
index: notSeeded,
}
return res
}
// Seed uses the given 64bit value to initialise the generator state.
// This method is part of the rand.Source interface.
func (mt *MT19937) Seed(seed int64) {
x := mt.state
x[0] = uint64(seed)
for i := uint64(1); i < n; i++ {
x[i] = 6364136223846793005*(x[i-1]^(x[i-1]>>62)) + i
}
mt.index = n
}
// SeedFromSlice uses the given slice of 64bit values to set the
// generator state.
func (mt *MT19937) SeedFromSlice(key []uint64) {
mt.Seed(19650218)
x := mt.state
i := uint64(1)
j := 0
k := len(key)
if n > k {
k = n
}
for k > 0 {
x[i] = (x[i] ^ ((x[i-1] ^ (x[i-1] >> 62)) * 3935559000370003845) +
key[j] + uint64(j))
i++
if i >= n {
x[0] = x[n-1]
i = 1
}
j++
if j >= len(key) {
j = 0
}
k--
}
for j := uint64(0); j < n-1; j++ {
x[i] = x[i] ^ ((x[i-1] ^ (x[i-1] >> 62)) * 2862933555777941757) - i
i++
if i >= n {
x[0] = x[n-1]
i = 1
}
}
x[0] = 1 << 63
}
// Uint64 generates a (pseudo-)random 64bit value. The output can be
// used as a replacement for a sequence of independent, uniformly
// distributed samples in the range 0, 1, ..., 2^64-1. This method is
// part of the rand.Source64 interface.
func (mt *MT19937) Uint64() uint64 {
x := mt.state
if mt.index >= n {
if mt.index == notSeeded {
mt.Seed(5489) // default seed, as in mt19937-64.c
}
for i := 0; i < n-m; i++ {
y := (x[i] & hiMask) | (x[i+1] & loMask)
x[i] = x[i+m] ^ (y >> 1) ^ ((y & 1) * matrixA)
}
for i := n - m; i < n-1; i++ {
y := (x[i] & hiMask) | (x[i+1] & loMask)
x[i] = x[i+(m-n)] ^ (y >> 1) ^ ((y & 1) * matrixA)
}
y := (x[n-1] & hiMask) | (x[0] & loMask)
x[n-1] = x[m-1] ^ (y >> 1) ^ ((y & 1) * matrixA)
mt.index = 0
}
y := x[mt.index]
y ^= (y >> 29) & 0x5555555555555555
y ^= (y << 17) & 0x71D67FFFEDA60000
y ^= (y << 37) & 0xFFF7EEE000000000
y ^= (y >> 43)
mt.index++
return y
}
// Int63 generates a (pseudo-)random 63bit value. The output can be
// used as a replacement for a sequence of independent, uniformly
// distributed samples in the range 0, 1, ..., 2^63-1. This method is
// part of the rand.Source interface.
func (mt *MT19937) Int63() int64 {
x := mt.state
if mt.index >= n {
if mt.index == notSeeded {
mt.Seed(5489) // default seed, as in mt19937-64.c
}
for i := 0; i < n-m; i++ {
y := (x[i] & hiMask) | (x[i+1] & loMask)
x[i] = x[i+m] ^ (y >> 1) ^ ((y & 1) * matrixA)
}
for i := n - m; i < n-1; i++ {
y := (x[i] & hiMask) | (x[i+1] & loMask)
x[i] = x[i+(m-n)] ^ (y >> 1) ^ ((y & 1) * matrixA)
}
y := (x[n-1] & hiMask) | (x[0] & loMask)
x[n-1] = x[m-1] ^ (y >> 1) ^ ((y & 1) * matrixA)
mt.index = 0
}
y := x[mt.index]
y ^= (y >> 29) & 0x5555555555555555
y ^= (y << 17) & 0x71D67FFFEDA60000
y ^= (y << 37) & 0xFFF7EEE000000000
y ^= (y >> 43)
mt.index++
return int64(y & 0x7fffffffffffffff)
}
// Read fills `p` with (pseudo-)random bytes. This method implements
// the io.Reader interface. The returned length `n` always equals
// `len(p)` and `err` is always nil.
func (mt *MT19937) Read(p []byte) (n int, err error) {
n = len(p)
for len(p) >= 8 {
val := mt.Uint64()
p[0] = byte(val)
p[1] = byte(val >> 8)
p[2] = byte(val >> 16)
p[3] = byte(val >> 24)
p[4] = byte(val >> 32)
p[5] = byte(val >> 40)
p[6] = byte(val >> 48)
p[7] = byte(val >> 56)
p = p[8:]
}
if len(p) > 0 {
val := mt.Uint64()
for i := 0; i < len(p); i++ {
p[i] = byte(val)
val >>= 8
}
}
return n, nil
}