use strict;
use warnings;
use File::Binary;
 
my $fb = File::Binary->new("myfile");
 
$fb->open("<bytes100MB");

#use Math::StdDev;
 
#my $d = new Math::StdDev();
#$d->Update(2);
#$d->Update(3);
#print $d->mean()."\n";

#use Math::Random qw(random_uniform random_set_seed_from_phrase random_binomial random_set_seed);

#random_set_seed_from_phrase(2);
#my @seed = (1 + int(rand(2147483562)), 1 + int(rand(2147483398)));
#random_set_seed(@seed);
my $n = shift;
$n ||= 1000;
#my @a = random_uniform($n);

#use Data::Dumper;

#print Dumper @a;

#my $nt = 1;
#my $p = 0.95;
my $p = 0.5;
#my @a = random_binomial($n, $nt, $p);
#my @a = map {rand > 0.5} 1..$n;
#my $rands = readthisfile('bytes37Mb.txt'); # 0..255
#my $rands = readthisfile('bins100Mb.txt'); # 0..255
#my @rands = @$rands;
#print "read\n";
use List::Util qw(sum);

#my $sum = sum(@a);
#my $mean = $sum/$n;
#print "$mean\n";
#my $sample = @a[int(rand($n))];
my @data;
#my @xaxis = (1..$n);
my $step = $n/10;
my @xaxis = map { $_/$step == int($_/$step) ? $_ : '' } 1 .. $n;
#my $i = 0;

#open my $in, "<", 'bytes100MB';
#open my $in, "<", 'binaryrandoms';
#binmode $in;
#my $offset = 0;

for my $howmany (1..$n) {

	my @list;
#	my @acopy = @a;
#	my $cont = '';
#	my $offset = 0;
	 
#	while (1) {
#		my $success = read $in, $cont, $howmany, length($cont);
#	  my $success = read $in, $cont, $howmany, $offset;
#	  my $success = read $in, $cont, $howmany, $offset + length($cont);
#	  croak $! if not defined $success;
#	  if (not $success) {
#			print "oops\n";
#			last;
#		}
#	}

#	$offset += $howmany;
#print "cont is $cont\n";

	for my $samples (1..$howmany) {
#		my $pos = int(rand(scalar @acopy));
#		my $thisone = $acopy[$pos];
#		splice @acopy, $pos, 1;
#		splice @acopy, $pos, 1;
#		push @list, $mean - $thisone;
#		push @list, $mean - (rand > 0.5);
#		push @list, (rand > 0.5 ? 0 : 1);
#		my $byte = vec $cont, $samples-1, 8;
#		print "$byte\n";
		push @list, ($fb->get_ui8() > 127.5 ? 0 : 1);
#		$i++;
	}

#	print $howmany, ' ', sum(@list)/$howmany, "\n";
	push @data, 0.5 - sum(@list)/$howmany;

}

$fb->close();
#close $in;
#print Dumper @data;
#print "$sample\n";

#use GD::Graph::bars;
use GD::Graph::Data;
use Carp;

#my $data = GD::Graph::Data->new([
#    ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
#    [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
#]) or croak GD::Graph::Data->error;

my $data = GD::Graph::Data->new([\@xaxis, \@data]) or croak GD::Graph::Data->error;

my $graph = GD::Graph::lines->new(800, 600);
#my $graph = GD::Graph::bars->new(800, 600);

$graph->set( 
#    x_label         => 'X Label',
#    y_label         => 'Y label',
  title           => $p,
	transparent     => 0,
	t_margin				=> 10,
	b_margin				=> 10,
	l_margin				=> 10,
	r_margin				=> 10,
	y_max_value     => 0.2,
	y_min_value     => -0.2,
) or croak $graph->error;
 
$graph->plot($data) or croak $graph->error;
 
my $file = $n."_255_bars$p.png";
#my $file = "barssplicy$p.png";
open(my $out, '>', $file) or croak "Cannot open '$file' for write: $!";
binmode $out;
print $out $graph->gd->png;
close $out;

use GD::Graph::lines;

sub readthisfile {
	my $filename = shift;
	open(my $fh, '<:encoding(UTF-8)', $filename)
		or croak "Could not open file '$filename' $!";
	my @rands;

	while (my $row = <$fh>) {
		chomp $row;
		push @rands, $row;
	}

	return \@rands;
}
