#Lagged Fibonacci Random Number Generator Generator

sub lfib{
    #use bigint;
    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
$rand3 = lfib(2**2, 17, 5, '+');

print &$rand(100) . "\n" . &$rand2() ."\n"  . &$rand3() ."\n";

my $sum = 0;

for (1..1000000){
    $sum += &$rand3();
}
print $sum."\n";

for (1..10){
    print  &$rand3()."\n";
}


#print 1000000/$sum, "\n";


