mardi 3 novembre 2015

Fibonacci shift register pseudo-random number generator

I am attempting to get the following code working for a Fibonacci shift register to generate pseudo-random numbers. Can't seem to get it working, so is(are) there any obvious issues(?)

Shared Function Main() As Integer
    Dim start_state As UShort = &HACE1UI ' Any nonzero start state will work.
    Dim lfsr As UShort = start_state
    Dim bit As UInteger
    Dim period As UInteger = 0

    Do While lfsr <> start_state
        ' taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 
        bit = ((lfsr >> 0) Xor (lfsr >> 2) Xor (lfsr >> 3) Xor (lfsr >> 5)) And 1
        lfsr = (lfsr >> 1) Or (bit << 15)
        period += 1
    Loop 

    Return 0
End Function

Last, does "period" need to be divided by a large integer to get U(0,1)'s?




Aucun commentaire:

Enregistrer un commentaire