Attribute VB_Name = "Module1"
Public Declare Function genrandm Lib "mt19937m" () As Double
Public Declare Sub sgenrandm Lib "mt19937m" (ByVal seed As Long)

Public Declare Function genrand Lib "mt19937" () As Double
Public Declare Sub sgenrand Lib "mt19937" (ByVal seed As Long)

Public Declare Function randomMT Lib "cokus" () As Double
Public Declare Sub srandMT Lib "cokus" (ByVal seed As Long)

Public Declare Function genrand_real2 Lib "mt19937ar" () As Double
Public Declare Function genrand_int32 Lib "mt19937ar" () As Long
Public Declare Sub init_by_array Lib "mt19937ar" (ByRef init_key As Long, ByVal key_length As Long)
Public Declare Sub init_genrand Lib "mt19937ar" (ByVal seed As Long)

Public Declare Function genrand_real2_cok Lib "mt19937ar-cok" Alias "genrand_real2" () As Double
Public Declare Function genrand_int32_cok Lib "mt19937ar-cok" Alias "genrand_int32" () As Long
Public Declare Sub init_by_array_cok Lib "mt19937ar-cok" Alias "init_by_array" (ByRef init_key As Long, ByVal key_length As Long)
Public Declare Sub init_genrand_cok Lib "mt19937ar-cok" Alias "init_genrand" (ByVal seed As Long)

Declare Function timeGetTime Lib "winmm" () As Long

Function test1()
Dim i As Double
Dim j As Long
Dim k As Long
Dim stime As Long
Dim result(5, 9) As Long
Dim init_key(3) As Long
init_key(0) = &H123
init_key(1) = &H234
init_key(2) = &H345
init_key(3) = &H456
    
For k = 0 To 9
    Debug.Print "test " & k + 1
    Randomize 4357
    stime = timeGetTime
    For j = 1 To 10000000
    i = Rnd()
    Next j
    result(0, k) = timeGetTime - stime
    Debug.Print "rnd         " & result(0, k) / 1000 & " sec"
    
    sgenrand 4357
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand
    Next j
    result(1, k) = timeGetTime - stime
    Debug.Print "mt19937(genrand)         " & result(1, k) / 1000 & " sec"
    
    sgenrandm 4357
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrandm
    Next j
    result(2, k) = timeGetTime - stime
    Debug.Print "mt19937m(genrandm)         " & result(2, k) / 1000 & " sec"

    srandMT 4357
    stime = timeGetTime
    For j = 1 To 10000000
    i = randomMT()
    Next j
    result(3, k) = timeGetTime - stime
    Debug.Print "cokus(randomMT)      " & result(3, k) / 1000 & " sec"

    init_by_array init_key(0), 4
    'init_genrand (4357)
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_real2()
    Next j
    result(4, k) = timeGetTime - stime
    Debug.Print "mt19937ar(genrand_real2)      " & result(4, k) / 1000 & " sec"

    init_by_array_cok init_key(0), 4
    'init_genrand_cok (4357)
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_real2_cok()
    Next j
    result(5, k) = timeGetTime - stime
    Debug.Print "mt19937ar-cok(genrand_real2_cok)      " & result(5, k) / 1000 & " sec"

Next k

    Debug.Print "Average"
    For k = 1 To 9
     result(0, 0) = result(0, 0) + result(0, k)
     result(1, 0) = result(1, 0) + result(1, k)
     result(2, 0) = result(2, 0) + result(2, k)
     result(3, 0) = result(3, 0) + result(3, k)
     result(4, 0) = result(4, 0) + result(4, k)
     result(5, 0) = result(5, 0) + result(5, k)
    Next
    
    Debug.Print "rnd         " & result(0, 0) / 10000 & " sec"
    Debug.Print "mt19937(genrand)         " & result(1, 0) / 10000 & " sec"
    Debug.Print "mt19937m(genrandm)         " & result(2, 0) / 10000 & " sec"
    Debug.Print "cokus(randomMT)      " & result(3, 0) / 10000 & " sec"
    Debug.Print "mt19937ar(genrand_real2)      " & result(4, 0) / 10000 & " sec"
    Debug.Print "mt19937-cok(genrand_real2_cok)      " & result(5, 0) / 10000 & " sec"

End Function

Function test2()
Dim j As Long
Dim init_key(3) As Long
init_key(0) = &H123
init_key(1) = &H234
init_key(2) = &H345
init_key(3) = &H456


    Worksheets(1).Cells(1, 1) = "Rnd"
    Worksheets(1).Cells(1, 2) = "genrand"
    Worksheets(1).Cells(1, 3) = "genrandm"
    Worksheets(1).Cells(1, 4) = "randomMT"
    Worksheets(1).Cells(1, 5) = "genrand_real2"
    Worksheets(1).Cells(1, 6) = "genrand_real2_cok"

    Randomize 4357
    For j = 2 To 1001
    Worksheets(1).Cells(j, 1) = Rnd()
    Next j
    
    sgenrand 4357
    For j = 2 To 1001
    Worksheets(1).Cells(j, 2) = genrand
    Next j
    etime = timeGetTime
    
    sgenrandm 4357
    For j = 2 To 1001
    Worksheets(1).Cells(j, 3) = genrandm
    Next j
    
    srandMT 4357
    For j = 2 To 1001
    Worksheets(1).Cells(j, 4) = randomMT()
    Next j

    init_by_array init_key(0), 4
    For j = 2 To 1001
    Worksheets(1).Cells(j, 5) = genrand_real2()
    Next j

    init_by_array_cok init_key(0), 4
    For j = 2 To 1001
    Worksheets(1).Cells(j, 6) = genrand_real2_cok()
    Next j

End Function

Function test3()
Dim i As Double
Dim j As Long
Dim k As Long
Dim stime As Long
Dim result(3, 9) As Long
Dim init_key(4) As Long
init_key(0) = &H123
init_key(1) = &H234
init_key(2) = &H345
init_key(3) = &H456

For k = 0 To 9
    Debug.Print "test " & k + 1
    init_by_array init_key(0), 4
    'init_genrand (4357)
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_int32()
    Next j
    result(0, k) = timeGetTime - stime
    
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_real2()
    Next j
    result(1, k) = timeGetTime - stime
    
    init_by_array_cok init_key(0), 4
    'init_genrand_cok (4357)
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_int32_cok()
    Next j
    result(2, k) = timeGetTime - stime
    
    stime = timeGetTime
    For j = 1 To 10000000
    i = genrand_real2_cok()
    Next j
    result(3, k) = timeGetTime - stime
    
    Debug.Print "mt19937ar(genrand_int32)         " & result(0, k) / 1000 & " sec"
    Debug.Print "mt19937ar(genrand_real2)         " & result(1, k) / 1000 & " sec"
    Debug.Print "mt19937ar-cok(genrand_int32_cok)         " & result(2, k) / 1000 & " sec"
    Debug.Print "mt19937ar-cok(genrand_real2_cok)         " & result(3, k) / 1000 & " sec"
Next k

    Debug.Print "Average"
    For k = 1 To 9
     result(0, 0) = result(0, 0) + result(0, k)
     result(1, 0) = result(1, 0) + result(1, k)
     result(2, 0) = result(2, 0) + result(2, k)
     result(3, 0) = result(3, 0) + result(3, k)
    Next
    Debug.Print "mt19937ar(genrand_int32)         " & result(0, 0) / 10000 & " sec"
    Debug.Print "mt19937ar(genrand_real2)         " & result(1, 0) / 10000 & " sec"
    Debug.Print "mt19937ar-cok(genrand_int32_cok)         " & result(2, 0) / 10000 & " sec"
    Debug.Print "mt19937ar-cok(genrand_real2_cok)         " & result(3, 0) / 10000 & " sec"

End Function