Thread: Looking for source code of Lagged Fibonacci generator

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    37

    Looking for source code of Lagged Fibonacci generator

    Does anyone know where I can find the source code of such a generator?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tried Google yet?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    37
    Yea I did tried google. They mostly have theory on LFGs. If you were able to find some code, then do share.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't have any, but I'll poke around.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lagged Fibonacci generator - Wikipedia, the free encyclopedia
    First hit, scroll down to "usage".

    Some open source implementations - go digging!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This was the best I found, outside Wikipedia:

    From: What is wrong with perl's antique random number generator


    Lagged Fibonacci Generator Generator
    Code:
    sub lfib{
        my ($m, $r, $k, $op, $seed) = @_;
        my (@x, $i);
        srand($seed ||time); #initialise state with rand
        for (0 .. $r){
            push @x, int(rand($m));
        }
        my $fn = "sub {
            \$i = (\$i + 1) % $r;
            \$x[\$i] = (\$x[\$i]  $op  \$x[(\$i-$k) % $r]) % $m;
            (shift || 1.0) * \$x[\$i] / $m;
        }\n";
        return eval($fn);   
    }
    
    $rand = lfib(2**48, 607, 273, '+');  #additive LFib, period 2 ** 638
    $rand2 = lfib(2**64, 1279, 861, '*');#multiplicative LFib, period 2 ** 1340
    
    print &$rand(100) . "\n" . &$rand2() ."\n";
    54.6312745966894
    0.0627432743424777

    Maybe this should use bignum, but it seems to work without.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Ansi C Fibonacci code stinks! obvious bug?
    By Hansie in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 12:29 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. How Can I compile 3 source code in 1 program ?
    By lord_cedrich in forum C Programming
    Replies: 8
    Last Post: 12-10-2006, 05:10 AM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM