#!/usr/bin/perl

use strict;
use warnings;

use Digest;

my $random_state;

sub srand{
    my $seed = shift || ("a very secret string " . time);
    $random_state = {
	digest => new Digest ("SHA-256"),
	counter => 0,
	waiting => [],
	bytes   => $seed
    };
}

sub rand{
    my $range = shift || 1.0;
    ::srand() unless defined $random_state;

    if (! @{$random_state->{waiting}}){
	$random_state->{digest}->reset();
	$random_state->{digest}->add("string together your entropy, state, magic etc" .
				     $random_state->{counter} ++ .
				     $random_state->{bytes});
	$random_state->{bytes} = $random_state->{digest}->digest();
	my @ints = unpack("L*", $random_state->{bytes});
	$random_state->{waiting} = \@ints;
    }
    my $int = shift @{$random_state->{waiting}};
    return $range * $int / 2**32;
}



#&srand(1);

my $sum = 0;

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

}

print $sum."\n";
