3
\$\begingroup\$

I had another question for my Perl class assignment.

Related question:

Frequency analysis for simultaneous dice rolls

use warnings;                
use diagnostics;
use GD::Graph::hbars;
use List::Util qw(max);
my $number_of_dice=0;
my $number_of_rolls=0;
my @total_sum_of_eyes;
my %count;
my $counter=0;

print "\n Please insert the number_of_dice: ";
chomp($number_of_dice=<STDIN>);
print "\n Please instert the number_of_rolls: ";
chomp($number_of_rolls=<STDIN>);

while ($counter< $number_of_rolls){
    push(@total_sum_of_eyes,roll($number_of_dice));
    $counter++;
}  
frequency(@total_sum_of_eyes);
representation(%count);

sub roll{
    my $number=0;
    my $sum_of_throw=0;
    $sum_of_throw = 0;
    for($i=0;$i<$number_of_dice;$i++){
        $number = int(rand(6) +1);
        $sum_of_throw+=$number;
    }
    return $sum_of_throw;
}
sub frequency{

    for($i=0;$i<$number_of_rolls;$i++){
        $count{$total_sum_of_eyes[$i]}++;
    }
    return;
}
sub representation{
    my @single_sum_of_eyes;
    my @frequency;
    print "\n the frequencys-distribution is: \n";
    print "\n total_sum_of_eyes\t frequency\t frequencys-distribution in %\n";
    foreach my $eyes (sort{$a <=> $b} keys %count){
        push(@frequency, $count{$eyes});
        push(@single_sum_of_eyes, $eyes);
        printf "\n\t $eyes\t\t\t  $count{$eyes}\t\t%g",($count{$eyes}/$number_of_rolls)*100;
    }
    print "\n";
    my $graph = GD::Graph::hbars->new(1600, 600);
$graph->set(
    x_label             => 'total_sum_of_eyes',
    y_label             => 'frequency',
    title               => 'frequencys-distribution',
    y_max_value         =>  max(@frequency)+1,
    y_tick_number       => 8,
    transparent         => 0,
    long_ticks          => 1,
) or die $graph->error;
my @data = (\@single_sum_of_eyes,\@frequency);
$graph->set( dclrs => [ qw(green) ] );
my $gd = $graph->plot(\@data) or die $graph->error;
open(IMG, '>gd_bars.gif') or die $!;
binmode IMG;
print IMG $gd->gif;
return;
}

frequency(@total_sum_of_eyes);
representation(%count);

Is this necessary?

foreach my $eyes (sort{$a <=> $b} keys %count){
        push(@frequency, $count{$eyes});
        push(@single_sum_of_eyes, $eyes);
        printf "\n\t $eyes\t\t\t  $count{$eyes}\t\t%g",($count{$eyes}/$number_of_rolls)*100;
    }

I can't seem to find a simple for-loop for this one.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Found a solution without foreach

@single_sum_of_eyes = sort{$a <=> $b} keys %count;
    for($j = 0; $j<scalar @single_sum_of_eyes; $j++)
    {
        push(@frequency,$count{$single_sum_of_eyes[$j]});
    }
    for($i=0; $i<scalar @frequency; $i++)
    {
    printf "\n\t$single_sum_of_eyes[$i]\t\t\t $frequency[$i]\t\t%g", ($frequency[$i]/$number_of_rolls)*100;
    }
    print "\n";
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.