-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt_random.go
43 lines (39 loc) · 1.11 KB
/
mt_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
package mt_random
type MersenneTwister struct {
index int
MT [624]int //624 * 32 - 31 = 19937
}
func NewMersenneTwister(seed int) MersenneTwister {
mt := MersenneTwister{}
mt.index = 0
mt.MT[0] = seed
//对数组的其它元素进行初始化
for i := 1; i < 624; i++ {
t := 1812433253*(mt.MT[i-1]^(mt.MT[i-1]>>30)) + i
mt.MT[i] = t & 0xffffffff //取最后的32位赋给MT[i]
}
return mt
}
func (mt *MersenneTwister) generate() {
for i := 0; i < 624; i++ {
// 2^31 = 0x80000000
// 2^31-1 = 0x7fffffff
y := (mt.MT[i] & 0x80000000) + (mt.MT[(i+1)%624] & 0x7fffffff)
mt.MT[i] = mt.MT[(i+397)%624] ^ (y >> 1)
if y&1 == 1 {
mt.MT[i] ^= 2567483615
}
}
}
func (mt *MersenneTwister) Rand() int {
if mt.index == 0 {
mt.generate()
}
y := mt.MT[mt.index]
y = y ^ (y >> 11) //y右移11个bit
y = y ^ ((y << 7) & 2636928640) //y左移7个bit与2636928640相与,再与y进行异或
y = y ^ ((y << 15) & 4022730752) //y左移15个bit与4022730752相与,再与y进行异或
y = y ^ (y >> 18) //y右移18个bit再与y进行异或
mt.index = (mt.index + 1) % 624
return y
}