#!/usr/bin/perl use strict; use warnings; use Storable; use Statistics::Descriptive; my @grid = @{ retrieve('grid.dat') }; my %param = %{ retrieve('param.dat') }; # rows and cols parameters my @contaminated_walks; for (1 .. 100) { my $num_walks = 100; my ($x, $y) = (int(rand $param{range_contx}), int(rand $param{range_conty})); my @walks; for (1 .. $num_walks) { my $steps = 100; my $infected; my $total_steps; #Inner loop to perform each step of a random walk for (1 .. $steps) { $total_steps += my $rand_steps = (1 + int( rand 6 )); last if $total_steps > $steps; my $random_num = rand; if($random_num < 0.25) { for (1 .. $rand_steps) { $x = ($x - 1) % $param{range_contx}; $infected += $grid[$x][$y] || 0; } } elsif ($random_num < 0.5) { for (1 .. $rand_steps) { $x = ($x + 1) % $param{range_contx}; $infected += $grid[$x][$y] || 0; } } elsif ($random_num < 0.75) { for (1 .. $rand_steps) { $y = ($y - 1) % $param{range_conty}; $infected += $grid[$x][$y] || 0; } } else { for (1 .. $rand_steps) { $y = ($y + 1) % $param{range_conty}; $infected += $grid[$x][$y] || 0; } } } push @walks, $infected; } push @contaminated_walks, scalar grep $_, @walks; } my $stat = Statistics::Descriptive::Sparse->new(); $stat->add_data(@contaminated_walks); printf "min-max %d-%d mean: %.1f std. deviation: %.1f count: %d\n", $stat->min, $stat->max, $stat->mean, $stat->standard_deviation, $stat->count; __END__ C:\Old_Data\perlp>perl t33.pl min-max 34-61 mean: 50.7 std. deviation: 5.6 count: 100