#!/usr/bin/perl

use strict;
use warnings;

my $random_state;

sub srand{
    my $seed = shift || time || 4357;
    my @a = ();
    for (1..256){
	use integer;
	push @a, $seed;
	$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) % 256;    
    my $a = $random_state->{array};    
    my $off = $random_state->{offset};   
    {
	use integer;
	$$a[$off] = $$a[($off - 55) % 256] +
                    $$a[($off - 119) % 256] +
                    $$a[($off - 179) % 256] +
                    $$a[$off];                    
    }
    my $r = $$a[$off] * $range /2**32;
    return $r + ($r < 0);
}



#&srand(1);

my $sum = 0;

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

}

print $sum."\n";
