Jump to content

Recommended Posts

Posted (edited)

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

Edited by Mosoo
Posted (edited)

Problem 1b)

unsigned e = y[index++];

Local $e = $Y[$index + 1]

Local $e = $Y[$index]

$index += 1

MSDN: Postfix Increment and Decrement Operators: ++ and --

(O yea, [Problem:1a] thats after you made $Y and $index Global. Or just Static if you don't mind using experimental au3 features.)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Posted (edited)

I red the article and edited these two problems

But still the code allways produces the same numbers ..

I would have edited this in the first post, but i cant o0

regardless i'll do it here :

#include<array.au3>

Opt("MustDeclareVars", 1)

local $random = mersenne_twister()
ConsoleWrite($random&@CRLF)

Func mersenne_twister()

    Const $N = 624
    Const $M = 397
    Const $A[2] = [0x0,0x9908b0df]
    Const $HI = 0x8000000
    Const $LO = 0x7fffffff
    Static $Y[$N]
    Static $index = $N+1

    if $index >= $N Then
        if $index > $N Then
            local $r = 9, $s = 3402
            local $i = 0
            while $i < $N
                $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)
            $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
            $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]
    $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
Edited by Mosoo
Posted (edited)

But still the code allways produces the same numbers ...

?

1) no it don't. (see code/data dump)

2) so will au3 random() if you re-start it with the same sRandom seed value. ($s might be the seed value, but thats just a guess. seed value -> $A[0])

--- --- 0 (array dump), $aTestData_Rf2:[14].
.   000 |I3| 1901524675 (0x00.00.00.00.71.56.F6.C3)
.   001 |I3| 2006825857 (0x00.00.00.00.77.9D.BB.81)
.   002 |I3| 467616265 (0x00.00.00.00.1B.DF.42.09)
.   003 |I3| 807373982 (0x00.00.00.00.30.1F.8C.9E)
.   004 |I3| 636322783 (0x00.00.00.00.25.ED.83.DF)
.   005 |I3| 782661099 (0x00.00.00.00.2E.A6.75.EB)
.   006 |I3| 840561414 (0x00.00.00.00.32.19.F3.06)
.   007 |I3| 1956678446 (0x00.00.00.00.74.A0.8B.2E)
.   008 |I3| 947680371 (0x00.00.00.00.38.7C.74.73)
.   009 |I3| 776372306 (0x00.00.00.00.2E.46.80.52)
.   010 |I3| 1372652264 (0x00.00.00.00.51.D1.02.E8)
.   011 |I3| 1390739564 (0x00.00.00.00.52.E5.00.6C)
.   012 |I3| 1046290337 (0x00.00.00.00.3E.5D.1F.A1)
.   013 |I3| 495281784 (0x00.00.00.00.1D.85.66.78)
--- --- 0 [0,0]
Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...