#!/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 ##```## #!/usr/bin/perl use strict; use warnings; use Storable; my \$number_contaminant = 100; my %param = (range_contx => 100, range_conty => 100 ); my @grid; for (1 .. \$number_contaminant) { #random positions of the contaminants, put the random number as integrer my \$x = int(rand \$param{range_contx}); my \$y = int(rand \$param{range_conty}); redo if \$grid[\$x][\$y]; # if already marked \$grid[\$x][\$y] = 1; } store \@grid, 'grid.dat'; store \%param, 'param.dat'; ##``````## #!/usr/bin/perl use strict; use warnings; use Storable; use Statistics::Descriptive; my @grid = @{ retrieve('grid.dat') }; my %param = %{ retrieve('param.dat') }; # range_contx and range_conty parameters my @contaminated_walks; for (1 .. 100) { my \$steps = 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 \$infected; #Inner loop to perform each step of a random walk for (1 .. \$steps) { my \$random_num = rand; my \$steps = (1 + int( rand 6 )); if(\$random_num < 0.25) { \$x = (\$x - \$steps) % \$param{range_contx}; } elsif (\$random_num < 0.5) { \$x = (\$x + \$steps) % \$param{range_contx}; } elsif (\$random_num < 0.75) { \$y = (\$y - \$steps) % \$param{range_conty}; } else { \$y = (\$y + \$steps) % \$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 "mean: %.f std. deviation: %.f count: %d\n", \$stat->mean, \$stat->standard_deviation, \$stat->count; __END__ C:\Old_Data\perlp>perl his_stat.pl mean: 60 std. deviation: 5 count: 100 ##``````## #!/usr/bin/perl use strict; use warnings; use List::Util qw/ shuffle /; use Statistics::Descriptive; my @contaminated_walks; for (1 .. 100) { my \$contaminated = 100; my \$grid_rows = 100; my \$grid_cols = 100; my \$steps = 100; my \$num_walks = 100; my @walks; for (1 .. \$num_walks) { my @grid = # 1's and 0's randomly ordered shuffle( (1) x \$contaminated, (0) x (\$grid_rows * \$grid_cols - \$contaminated) ); # for each walk, 'grep' gets the count of contaminated cells push @walks, scalar grep \$_, map \$grid[rand @grid], 1 .. \$steps; } push @contaminated_walks, scalar grep \$_, @walks; } my \$stat = Statistics::Descriptive::Sparse->new(); \$stat->add_data(@contaminated_walks); printf "mean: %.f std. deviation: %.f count: %d\n", \$stat->mean, \$stat->standard_deviation, \$stat->count; __END__ C:\Old_Data\perlp>perl my_stat.pl mean: 64 std. deviation: 5 count: 100 ```