#!/usr/bin/perl

use strict;
use warnings;

use Crypt::OpenSSL::Random;

my $random_state;

sub srand{
    use integer;
    my $seed = shift || time || 5489;
    while (! Crypt::OpenSSL::Random::random_seed(pack('j', $seed))){
	$seed = (1103515245 * $seed + 12345);
    }
    $random_state = [];
}

sub rand{
    my $range = shift || 1.0;
    ::srand() unless $random_state; #Crypt::OpenSSL::Random::random_status();
    if (! @$random_state){
	my $bytes = Crypt::OpenSSL::Random::random_bytes(256);
	@$random_state = unpack("L*", $bytes);
    }
    my $int = shift @$random_state;
    return $range * $int / 2**32;
}


my $sum = 0;

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

print $sum."\n";
