I am attempting to randomly infect a certain number of people in a population notated by @pop (not shown). The variable $pop stores the index size of the array that contains my population. As of now this piece of code works but is still failing in one small area. Everytime it encounters a second draw that is equal to the first draw, it produces a number and prints it although it doesn't belong to either the first or second draw.
sub randompick_infect { my $range = $pop; $rand = int(rand($range)); print "$rand\n"; $pop[$rand]{status} = 1; } print "How many random infections? \n"; my $answerinfect = <>; for ($i=1; $i<$answerinfect; $i++){ randompick_infect; $firstcontact = $rand; print "first contact $firstcontact\n"; do{ randompick_infect; $secondcontact = $rand; print "second contact $secondcontact\n"; if ($secondcontact == $firstcontact){ randompick_infect; } else { $j=-1; } }while($j!=-1); } printarray; print "You have infected $answerinfect people.\n";
Any help appreciated.
In reply to infinite loop does not stop by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |