在Mersenne Twister的VB.NET实现中,我得到一个超出范围的异常,它会被定期抛出(下面的错误图片截图):
Private Shared mt(312 - 1) As ULong
Private Shared mti As ULong = 312 + 1
Private Function genrand64_int64() As ULong
On Error GoTo errhandler
Dim i As Long
Dim x As ULong
Static mag01() As ULong = {0UL, MATRIX_A}
If mti >= NN Then ' generate NN words at one time
' if init_genrand64() has not been called,
' a default initial seed is used
If mti = NN + 1 Then
init_genrand64(5489UL)
End If
i = 0
Do While i < NN - MM
x = (mt(i) And UM) Or (mt(i + 1) And LM)
mt(i) = mt(i + MM) Xor (x >> 1) Xor mag01(CInt(x And 1UL))
i += 1
Loop
Do While i < NN - 1
x = (mt(i) And UM) Or (mt(i + 1) And LM)
mt(i) = mt(i + (MM - NN)) Xor (x >> 1) Xor mag01(CInt(x And 1UL))
i += 1
Loop
x = (mt(NN - 1) And UM) Or (mt(0) And LM)
mt(NN - 1) = mt(MM - 1) Xor (x >> 1) Xor mag01(CInt(x And 1UL))
mti = 0
End If
x = mt(mti) '<<<<<<<<<<<HERE<<<<<<<<<<<<<<<<<<<<<<
mti += 1
x = x Xor (x >> 29) And &H5555555555555555UL
x = x Xor (x << 17) And &H71D67FFFEDA60000UL
x = x Xor (x << 37) And &HFFF7EEE000000000UL
x = x Xor (x >> 43)
Return x
End Function
具体而言,例外情况如下:
请注意,mt()向量使用312个元素检出OK,并且在异常期间,mti = 42,86或212的索引值,在312的范围内.x的值是否太大?< / p>
init是:
Sub init_genrand64(ByVal seed As ULong)
mt(0) = seed
For mti = 1 To NN - 1
mt(mti) = (6364136223846793005UL * (mt(mti - 1) Xor (mt(mti - 1) >> 62)) + mti)
Next mti
End Sub