Created
April 18, 2017 12:45
-
-
Save sskwwskwww/6ab932cc5e9c5c50d271f03bc90c5695 to your computer and use it in GitHub Desktop.
MTのfill array的な奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class MT | |
Private mt() As UInt32 | |
Private idx As Integer | |
Public Sub New(ByVal s As UInt32, ByVal max As Integer) | |
Init(s, max) | |
End Sub | |
Public Sub Init(ByVal s As UInt32, ByVal max As Integer) | |
If max < 623 Then | |
max = 623 | |
End If | |
ReDim mt(max) | |
idx = 0 | |
'init | |
mt(0) = s | |
Dim i As Integer | |
For i = 1 To 623 | |
mt(i) = (1812433253UI * (mt(i - 1) Xor (mt(i - 1) >> 30)) + CUInt(i)) | |
Next | |
'fill | |
i = 0 | |
Dim t As UInt32 | |
While i < 624 - 397 | |
t = (mt(i) And &H80000000UI) Or (mt(i + 1) And &H7FFFFFFFUI) | |
mt(i) = mt(i + 397) Xor (t >> 1) | |
If (t And 1UI) = 1 Then | |
mt(i) = mt(i) Xor &H9908B0DFUI | |
End If | |
i += 1 | |
End While | |
While i < 624 - 1 | |
t = (mt(i) And &H80000000UI) Or (mt(i + 1) And &H7FFFFFFFUI) | |
mt(i) = mt(i + 397 - 624) Xor (t >> 1) | |
If (t And 1UI) = 1 Then | |
mt(i) = mt(i) Xor &H9908B0DFUI | |
End If | |
i += 1 | |
End While | |
While i < 624 | |
t = (mt(i) And &H80000000UI) Or (mt(i + 1 - 624) And &H7FFFFFFFUI) | |
mt(i) = mt(i + 397 - 624) Xor (t >> 1) | |
If (t And 1UI) = 1 Then | |
mt(i) = mt(i) Xor &H9908B0DFUI | |
End If | |
i += 1 | |
End While | |
While i <= max | |
t = (mt(i - 624) And &H80000000UI) Or (mt(i + 1 - 624) And &H7FFFFFFFUI) | |
mt(i) = mt(i + 397 - 624) Xor (t >> 1) | |
If (t And 1UI) = 1 Then | |
mt(i) = mt(i) Xor &H9908B0DFUI | |
End If | |
i += 1 | |
End While | |
'tempering | |
For i = 0 To max | |
t = mt(i) | |
t = t Xor (t >> 11) | |
t = t Xor ((t << 7) And &H9D2C5680UI) | |
t = t Xor ((t << 15) And &HEFC60000UI) | |
mt(i) = t Xor (t >> 18) | |
Next | |
End Sub | |
Public Sub Seek(ByVal n As Integer) | |
idx = n | |
End Sub | |
Public Function GetMT() As UInt32 | |
Dim s As UInt32 = mt(idx) | |
idx += 1 | |
Return s | |
End Function | |
Public Sub Advance() | |
idx += 1 | |
End Sub | |
Public Sub Advance(ByVal n As Integer) | |
idx += n | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment