How to simulate rolling a dice 6000 times with PERL?

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.


Related Blogs

How do I pass a list/array of nubmers to the the 2 subroutines (add and multiply) as a reference in perl prgrm

An example I was given was
$aref = \@array; # $aref now holds a reference to @array;

So like if I had
$xy = $aref; $xy now holds a reference to @array

How do I put that in a program like this:

# If first argument is add, run add function; if multiply, run multiply
# function, otherwise warn user and end script
$cmd = shift @ARGV;
if ($cmd =~ /add|multiply/) {
$rtn = &$cmd(@ARGV);
print "The result is: $rtn\n";
}
else {
print "\nInvalid request, please use \"add\" or \"multiply\"\n";
print "followed by some numbers.\n";
}

# Subroutine definitions #
sub add
{
my $sum = 0;
$sum += $_ foreach @_;
return $sum;
}
sub multiply
{
my $prod = 1;
$prod *= $_ foreach @_;
return $prod;
}


Related Blogs

In Perl, how do I add elements of an array using a loop?

Using Perl:

I tried using a for loop but it keeps printing out the last element in the array with the total. (i.e. "410" 4 is the last element and 10 is the total)

for($i=$#ARGV; $i>=0; $i–)
{$sum += $ARGV[$i];}
print "$sum\n";

Furthermore, how would I pass @ARGV to a subroutine and what would the "function" call in the main program look like?


Related Blogs

PERL – how can I substitute text from another file into my perl script?

I am not talking about creating a subroutine.

I simply want to automatically substitute text from other files into perl scripts at certain places before I run it.

Is there a simple way of doing this?

(Note, I cannot do this with subroutines, since I want to substitute the beginning of a loop from one place, the middle from another, and the end from yet another)


Related Blogs

What causes a "Bareword 'Module' not allowed while strict subs" error in Perl?

For example, if I run a program with a module called NetCDF::No_Write independently, it works fine. But when I stick it into a subroutine and within a foreach loop (so that it will do it for every element in an array that’s passed into the subroutine) I get a "Bareword NetCDF::No_Write not allowed while strict subs at line ###" error.

Just curious what kind of things I might want to look for.


Related Blogs

legitimate work from home jobs | Event Planning Bay Area | rent video games | Homeloans | Plumbing Courses in Manchester | increase fertility | Green Drink | Photo Keyring | Vitamin D Deficiency Symptoms | skip bags | Order Fulfillment Software | virtual receptionist | Educators Credit | bodybuilding workouts | window cleaning