#!/usr/bin/perl

use strict;
use warnings;

my $random_state;

my $MOD = 0x7fffffff;

sub srand{
    my $seed = shift || time || 4357;
    my @a = ();
    for (1..5){
	use integer;
	push @a, ($seed & $MOD) % $MOD;
	print (($seed & $MOD) % $MOD, "\n");
	$seed *= 69069;
    }
    $random_state = {
	offset => 0,   	
	array => \@a
    };    
    for (1..6){
	print rand(), "\n"; #get and throw away a few goes.
    }
}

sub rand{
    my $range = shift || 1.0;
    ::srand() unless defined $random_state;
    $random_state->{offset} = ($random_state->{offset} + 1) % 5;    
    my $a = $random_state->{array};    
    my $off = $random_state->{offset};   
    {
	#use integer;
	$$a[$off] = ((107374182 * $$a[($off -  1) % 5] + 
		      104480 * $$a[$off])) % $MOD;
    }
    return $$a[$off] * $range / $MOD;
}



#&srand(1);

my $sum = 0;

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

}

print $sum."\n";
