Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: infinite loop does not stop
by frozenwithjoy (Priest) on Jul 14, 2012 at 20:23 UTC | |
|
Re: infinite loop does not stop
by Athanasius (Cardinal) on Jul 15, 2012 at 08:36 UTC | |
|
Re: infinite loop does not stop
by aitap (Curate) on Jul 14, 2012 at 20:12 UTC | |
|
Re: infinite loop does not stop
by Anonymous Monk on Jul 15, 2012 at 14:11 UTC | |
|
Re: infinite loop does not stop
by linuxkid (Sexton) on Jul 15, 2012 at 15:07 UTC |