As I don't have enough information about PB's PRNG, I have converted the Mersenne Twister to PB. The generated numbers differ from the original, though. Hopefully just because PB doesn't have unsigned longs, but I can't be sure.
I have done some tests, and everything looks fine. But maybe some of you have different tests, and can spot errors?
Here, PBs Random() is 3-4 times faster than the MT-version, but the Mersenne Twister has a very long period (2^19937-1, time until it repeats), and generates 'better' random numbers than most other PRNGs (I have not enough information to compare it to PBs, though).
And with ASM, it's speed could be improoved by a large margin, so there probably wouldn't be to much difference to PBs PRNG.
Code: Select all
;             Mersenne Twister 
;    pseudo-random number generator (PRNG)
;
; PureBasic conversion by Hades, May 2006, 
; of... 
;
; /*
; A C-program for MT19937, with initialization improved 2002/1/26.
; Coded by Takuji Nishimura and Makoto Matsumoto.
; 
; Before using, initialize the state by using init_genrand(seed)  
; or init_by_array(init_key, key_length).
; 
; Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
; All rights reserved.                           
; Copyright (C) 2005, Mutsuo Saito,
; 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.
; 
; 
; Any feedback is very welcome.
; http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
; email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
; */
     
; Period parameters 
#MT_N = 624
#MT_M = 397
#MT_MatrixA = $9908B0DF     ; constant vector a
#MT_UpperMask = $80000000   ; most significant w-r bits
#MT_LowerMask = $7FFFFFFF   ; least significant r bits
Global Dim MT_mt.l(#MT_N - 1)   ; the array for the state vector
Global MT_mti.l = #MT_N + 1     ; mti==N+1 means mt[N] is not initialized
Global Dim MT_mag01(1)  ; MT_mag01[x] = x * #MT_MatrixA  for x=0,1
MT_mag01(0) = 0
MT_mag01(1) = #MT_MatrixA
Procedure MT_init_genrand(Seed.l) ; initializes the Mersenne Twister with a seed (-2147483648 <= Seed <= 2147483647)
  MT_mt(0) = Seed
  For MT_mti = 1 To #MT_N-1
    MT_mt(MT_mti) = 1812433253 * (MT_mt(MT_mti-1) ! (((MT_mt(MT_mti-1) >> 1) & #MT_LowerMask) >> 29)) + MT_mti
  Next
EndProcedure
; initialize by an array with array-length
; init_key is the array for initializing keys
; key_length is its length 
Procedure MT_init_by_array(*init_key.l, key_length.l)
  Protected i.l, j.l, k.l, n.l
  MT_init_genrand(19650218)
  i = 1 : j = 0
  k = key_length
  If k > #MT_N 
    k = #MT_N
  EndIf
  For n = k To 0 Step -1
    MT_mt(i) = (MT_mt(i) ! ((((MT_mt(i-1) >> 1) & #MT_LowerMask) >> 29) * 1664525)) + PeekL(*init_key + 4 * j) + j
    i + 1 : j + 1
    If i >= #MT_N 
      MT_mt(0) = MT_mt(#MT_N-1)
      i = 1
    EndIf
    If j >= key_length
      j = 0
    EndIf
  Next
  For k = #MT_N - 1 To 0 Step -1
    MT_mt(i) = (MT_mt(i) ! (MT_mt(i-1) ! (((MT_mt(i-1) >> 1) & #MT_LowerMask) >> 29) * 1566083941)) - i
    i + 1
    If i >= #MT_N
      MT_mt(0) = MT_mt(#MT_N - 1)
      i = 1
    EndIf
  Next
  MT_mt(0) = #MT_UpperMask
EndProcedure
Procedure.l MT_genrand_int32() ; Returns a random long (0 .. $7FFFFFFF)
  Protected y.l, kk.l, yh.l
  If MT_mti >= #MT_N
    If MT_mti = #MT_N + 1   ; if MT_init_genrand() has not been called,
      MT_init_genrand(5489) ; a default initial seed is used
    EndIf
    For kk = 0 To (#MT_N - #MT_M) - 1
      y = (MT_mt(kk) & #MT_UpperMask) | (MT_mt(kk+1) & #MT_LowerMask)
      MT_mt(kk) = MT_mt(kk+#MT_M) ! ((y >> 1) & #MT_LowerMask) ! MT_mag01(y & 1)
    Next
    For kk = #MT_N - #MT_M To #MT_N - 2
      y = (MT_mt(kk) & #MT_UpperMask) | (MT_mt(kk+1) & #MT_LowerMask)
      MT_mt(kk) = MT_mt(kk+(#MT_M - #MT_N)) ! ((y >> 1) & #MT_LowerMask) ! MT_mag01(y & 1)
    Next
    y = (MT_mt(#MT_N-1) & #MT_UpperMask) | (MT_mt(0) & #MT_LowerMask)
    MT_mt(#MT_N-1) = MT_mt(#MT_M-1) ! ((y >> 1) & #MT_LowerMask) ! MT_mag01(y & 1)
    MT_mti = 0
  EndIf
  y = MT_mt(MT_mti)
  MT_mti + 1
  ; Tempering
  y = y ! (((y >> 1) & #MT_LowerMask) >> 10)
  yh = y << 6
  If yh And $40000000
    y = y ! (((yh << 1) | #MT_UpperMask) & $9D2C5680)
  Else
    y = y ! ((yh << 1) & $9D2C5680)
  EndIf
  yh = y << 14
  If yh And $40000000
    y = y ! (((yh << 1) | #MT_UpperMask) & $EFC60000)
  Else
    y = y ! ((yh << 1) & $EFC60000)
  EndIf
  y = y ! (((y >> 1) & #MT_LowerMask) >> 17)
  
  ProcedureReturn y & #MT_LowerMask
EndProcedure
Procedure.f MT_Rnd() ; Returns a random float (0.0 .. 1.0)
  ProcedureReturn MT_genrand_int32() * (1.0 / 2147483647.0)
EndProcedure
Procedure.l MT_Random(Max.l) ; Returns a random long (0 .. Max, 1 <= Max <= 2147483647) 
  If Max <= 0
    ProcedureReturn 0
  EndIf
  ProcedureReturn MT_genrand_int32() % (Max + 1)
EndProcedure





