I’m trying to do an assignment about Subroutines in PERL. I need to simulate rolling a dice 6000 times using the srand() and tally the results in a tabular format.
Here is an excerpt of the code I produced:
#Generates rolling a dice and counts the occurences which number shows up
&roll_dice
my @whichnum = qw(1 2 3 4 5 6 3 4 5 5 2 2 4 4 6);
my %count;
#####random generating rolling a die 6000 times, and trying to run the program once####
foreach($count) {
if ($roll == 1 && <>6000) {
foreach (@whichnum) {
if (exists $count ($_)++;
else {
$count{$_} = 1;
}
}
}
}
#counts the number of occurences which number shows up from #rolling a die
foreach (@whichnum) {
if (exists $count ($_)++;
else {
$count{$_} = 1;
}
}
#counts how many occurences and shows result on tabular format
foreach (keys %count) {
print "$_ \t $count{$_} \n";
foreach(
sub roll_dice {
$roll = &roll_dice
}
I am just doing 1 side of rolling the die to see if this program works. However, I may miss some logic and trying to get this program to go to the right direction for this particular assignment. Any help is appreciated before Wednesday, October 15,2008.












