Hi coders, I know this is sensless and the random() func still uses this algo. but i want to give it a try cause of exercise, and I failed .. I wanted to write the Mersenne-twister in AutoIt language but it did not work.. i think the problem is the work with unsigned numbers.. Can anyone take a look at the original C -source and help me? My Code so far : ยด#include<array.au3>
Opt("MustDeclareVars", 1)
for $i = 0 to 5
local $random = mersenne_twister()
ConsoleWrite($random&@CRLF)
next
Func mersenne_twister()
Const $N = 624
Const $M = 397
Dim $A[2] = [0x0,0x9908b0df]
Const $HI = 0x8000000
Const $LO = 0x7fffffff
Dim $Y[$N]
local $index = $N+1
if $index >= $N Then
if $index > $N Then
local $r = 9, $s = 3402
local $i = 0
while $i < $N
;~ $i+=1
$r = 509845221 * $r + 3
$s *= $s+1
$y[$i] = $s + BitShift($r,10)
$i+=1
wend
EndIf
;~ _ArrayDisplay($y)
Local $h, $k = 0
while $k < ($N-$M)
;~ $k +=1
$h =BitOR(BitAND($y[$k],$HI),BitAND($y[$k+1],$LO))
$y[$k] = BitXOR($y[$k+$M] , BitShift($h,1) , $A[BitAND($h,1)])
$k +=1
WEnd
$k = $N-$M
while $k < $N-1
;~ $k +=1
$h =BitOR(BitAND($y[$k],$HI),BitAND($y[$k+1],$LO))
$y[$k] = BitXOR($y[$k+($M-$N)] , BitShift($h,1) , $A[BitAND($h,1)])
$k +=1
wend
$h = BitOR(BitAND($y[$N-1],$HI),BitAND($y[0],$LO))
$y[$n-1] = BitXOR($y[$M-1],BitShift($h,1),$A[BitAND($h,1)])
$index = 0
EndIf
Local $e = $y[$index+1]
;~ tempering:
$e = BitXOR($e,BitShift($e,11))
$e = BitXOR($e,BitAND(BitShift($e,-7),0x9d2c5680))
$e = BitXOR($e,BitAND(BitShift($e,-15),0xefc60000))
$e = BitXOR($e,BitShift($e,18))
Return $e
EndFunc Greetings