Code: Select all
/'
A C-program for MT19937-64 (2004/9/29 version).
Coded by Takuji Nishimura and Makoto Matsumoto.
This is a 64-bit version of Mersenne Twister pseudorandom number
generator.
Before using, initialize the state by using init_genrand64(seed)
or init_by_array64(init_key, key_length).
Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
References:
T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
ACM Transactions on Modeling and
Computer Simulation 10. (2000) 348--357.
M. Matsumoto and T. Nishimura,
``Mersenne Twister: a 623-dimensionally equidistributed
uniform pseudorandom number generator''
ACM Transactions on Modeling and
Computer Simulation 8. (Jan. 1998) 3--30.
Any feedback is very welcome.
http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
'/
'sources at
'http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
declare sub main() 'as long
main()
sleep
end
declare sub init_genrand64(byval seed as ulongint)
declare sub init_by_array64(byval init_key as ulongint ptr, byval key_length as ulongint)
declare function genrand64_int64() as ulongint
#define genrand64_int63() clngint(genrand64_int64() shr 1)
#define genrand64_real1() cdbl((genrand64_int64() shr 11) * (1.0 / 9007199254740991.0))
#define genrand64_real2() cdbl((genrand64_int64() shr 11) * (1.0 / 9007199254740992.0))
#define genrand64_real3() cdbl(((genrand64_int64() shr 12) + 0.5) * (1.0 / 4503599627370496.0))
const NN = 312
const MM = 156
const MATRIX_A = &hB5026F5AA96619E9ull
const UM = &hFFFFFFFF80000000ull
const LM = &h7FFFFFFFull
dim shared mt(0 to NN-1) as ulongint
dim shared mti as long = NN + 1
private sub main() 'as long
dim i as long
dim init(0 to 3) as ulongint = {&h12345ULL, &h23456ULL, &h34567ULL, &h45678ULL}
dim length as ulongint = 4
init_by_array64(@init(0), length)
print "1000 outputs of genrand64_int64()"
for i=0 to 999
print using "#####################";genrand64_int64();
If i Mod 5=4 Then
print
end if
next
print
print "1000 outputs of genrand64_real2()"
for i=0 to 999
print using "#.######## "; genrand64_real2();
If i Mod 5=4 Then
print
end if
next
'return 0
end sub 'function
private sub init_genrand64(byval seed as ulongint)
mt(0) = seed
For mti = 1 To NN - 1
mt(mti) = (6364136223846793005ULL * (mt(mti-1) Xor (mt(mti-1) shr 62)) + mti)
Next
end sub
private sub init_by_array64(byval init_key as ulongint ptr, byval key_length as ulongint)
dim i as ulongint
dim j as ulongint
dim k as ulongint
init_genrand64(19650218ULL)
i = 1
j = 0
k = iif(NN > key_length, NN, key_length)
Do While k <> 0
mt(i) = (mt(i) Xor ((mt(i-1) Xor (mt(i-1) shr 62)) * 3935559000370003845ULL)) + init_key[j] + j ' non linear
i += 1
j += 1
If i>=NN Then
mt(0) = mt(NN-1)
i = 1
End If
If j>=key_length Then
j = 0
End If
k -= 1
Loop
for k = NN-1 to 1 step -1
mt(i) = (mt(i) Xor ((mt(i-1) Xor (mt(i-1) shr 62)) * 2862933555777941757ULL)) - i ' non linear
i += 1
If i>=NN Then
mt(0) = mt(NN-1)
i = 1
End If
next
mt(0) = 1ULL shl 63
end sub
private function genrand64_int64() as ulongint
dim i as long
dim x as ulongint
static mag01(0 to 1) as ulongint = {0ULL, &hB5026F5AA96619E9ULL}
if mti >= NN then
if mti = (NN + 1) then
init_genrand64(5489ULL)
end if
for i=0 to NN-MM-1
x = (mt(i) And UM) Or (mt(i+1) And LM)
mt(i) = mt(i+MM) Xor (x shr 1) Xor mag01(CInt(x And 1ULL))
next
Do While i<NN-1
x = (mt(i) And UM) Or (mt(i+1) And LM)
mt(i) = mt(i+(MM-NN)) Xor (x shr 1) Xor mag01(CInt(x And 1ULL))
i += 1
Loop
x = (mt((NN - 1)) and &hFFFFFFFF80000000ULL) or (mt(0) and &h7FFFFFFFULL)
mt((NN - 1)) = (mt((MM - 1)) xor (x shr 1)) xor mag01(clng(x and 1ULL))
mti = 0
end if
x = mt(mti)
mti += 1
x xor= (x shr 29) and &h5555555555555555ULL
x xor= (x shl 17) and &h71D67FFFEDA60000ULL
x xor= (x shl 37) and &hFFF7EEE000000000ULL
x xor= x shr 43
return x
end function