minorsecond
New User
Sep 23, 2012, 12:17 PM
Post #1 of 10
(2660 views)

Creating a 100x100 grid in perl

Can't Post


Hey everyone! I'm writing code for an epidemiology class in which I'm supposed to have a random walker take 100 steps and determine the probability that it will become infected by walking over a contaminant. The grid on which the walker walks is 100x100. The walker is to begin at a random location on the grid. My partner has done the bulk of the work so I'm trying to finish it up but I just can't seem to figure out how to limit the grid size. How do I make it so that the walker will either not make a move if the step would land it outside of the grid, or roll it over the other other side of the grid if it steps over the border? Please don't think I'm asking you to do my work for me. I want to learn how to do this as I may use this again in the future. Thank you so much!
#!/usr/bin/perl w # use strict; use warnings; # purpose: 2 dimensional random walk + Probability of infection # First Computational epidemiology homework, Sept 26 2012. # usage: perl HW1.pl my $file_out = ">HW1.out"; my $cont_out = ">cont.out"; my $NO_OF_STEPS; my $NO_OF_RUNS; # use the perl open function to open the files open(FILE_OUT, $file_out) or die "Could not open write $file_out, program halting."; open(CONT_OUT, $cont_out) or die "Could not open $cont_out, program halting."; # Number of steps in each walk $NO_OF_STEPS = 100; # # Number of runs or times the random walk is done $NO_OF_RUNS = 100; #declarations of variables my %hash_walker_pos; #hash to store X positions my $xpos_walker = 0; my $ypos_walker = 0; my %hash_cont_pos; #hash to store Y positions my $ypos_cont; my $xpos_cont; my $xpos; my $ypos; my $dist_from_origin; my $expected_dist; my $count = $NO_OF_RUNS; #counter my @all_expected_dist; my $i; my $j; my $k; my $random_num; my @sum_cont_dist; my $expected_dist_cont; my $rangex = 2; my $rangey = 2; my $range_contx = 100; my $range_conty = 100; my $random_num_contx; my $random_num_conty; my $random_num_x; my $random_num_y; my @xpos_Arr; my @ypos_Arr; my $count_found = 0; my $count_not_found =0; my $count_run = 0; my $count_total = 0; my $probability; # make STDIN for number of steps # print "Number of steps? \n"; # my $NO_OF_STEPS = <>; # # Make STDIN for number of steps # print "Number of runs? \n"; # my $NO_OF_RUNS = <>; #make STDIN for the number of contaminants print "Number of contaminants?\n"; my $number_contaminant = <>; #put srand funtion { srand (3) } #Create contaminants random positions for ($k = 0; $k <$number_contaminant; $k++) { #random positions of the contaminants, put the random number as integrer $random_num_contx = int(rand($range_contx)); $random_num_conty = int(rand($range_conty)); #Storing contaminant positions into hash $xpos_cont = $random_num_contx; $ypos_cont = $random_num_conty; $hash_cont_pos{$xpos_cont}= $ypos_cont; #Print contaminant file. print CONT_OUT "$k Cont_xpos\t".$xpos_cont. "\t Cont_ypos\t".$ypos_cont."\n"; } #Seed random number generator for walker { srand (5) } #Outer while loop to run specified number of walks while($count > 0) { #initialize this values here to start over on each run @xpos_Arr = (); @ypos_Arr = (); $xpos_walker = 0; $ypos_walker = 0; push(@xpos_Arr,"0"); push(@ypos_Arr,"0"); #Inner loop to perform each step of a random walk for ($i = 0 ; $i < $NO_OF_STEPS; $i++) { #random positions of the walker, the random number is integrer $random_num_x = int(rand($rangex)); $random_num_y = int(rand($rangey)); #Storing xpos, ypos into designated arrays push(@xpos_Arr,$random_num_x); push(@ypos_Arr,$random_num_y); #creating x and y current position after sum the previous and store in hash if($i == 0) { #this is only for the first position $xpos_walker = $xpos_Arr[$i+1]; $ypos_walker = $ypos_Arr[$i+1]; } else { $xpos_walker = $xpos_Arr[$i+1] + $xpos_walker; $ypos_walker = $ypos_Arr[$i+1] + $ypos_walker; $hash_walker_pos{$xpos_walker}= $ypos_walker; } #print the position of the walker each time print FILE_OUT "xpos = $xpos_Arr[$i+1] ypos = $ypos_Arr[$i+1]\n"; print FILE_OUT"$i xpos\t".$xpos_walker. "\typos\t".$ypos_walker."\n"; } $count ; #compare both hashes with positions to see if walker gets the contaminant my $found = 0; my $count_found = 0; foreach $xpos_cont (keys %hash_cont_pos) { if(exists($hash_walker_pos{$xpos_cont})) { if ($hash_walker_pos{$xpos_cont} eq $hash_cont_pos{$xpos_cont}) { $found=1; #print "found equal position\n"; $count_found ++; $count_total ++; } } if ($found != 1) { #print "not equal position\n"; $count_not_found ++; } else { $found=0; } } if($count_found > 0) { $count_run ++; } } #Calculate Probability { $probability = ($count_run / 100); } print "Total steps in all runs that infected the walker= $count_total\n"; print "Runs that infected walker = $count_run\n"; print "Probability of infection = $probability\n"; #calculate probability #Averaging distance to compute expected distance #$expected_dist = $expected_dist/$NO_OF_RUNS; #print "Distance from origin: $expected_dist\n"; #print "cont n1 = $n1\ncont n5 = $n5[0]\ncont n10 = $n10[0]\ncont n20 = $n20[0]\ncont n50 = $n50[0]\ncont n100 = $n100[0]\n"; close FILE_OUT;
