"This question was given to be in the first week of a introduction to Perl class ..."
Given this information, it seems unlikely that you were expected to use a variety of functions from core and CPAN modules; however, I could be entirely wrong about that. When stuck on a homework problem such as this, it helps if we know exactly what you were learning. Context is also useful: for instance, "I'm learning programming for the first time at school", "I'm studying bioinformatics at university", and so on. Also take a look at "How do I post a question effectively?" for more guidelines on what information to provide with your question.
Putting the implementation aside, the actual problem to be solved is unclear. As I write this, I see there's already two interpretations; and I have a third. Showing a clear example of the type of results you're expecting would really help.
Anyway, this sounded like a fairly basic problem, that's used in programming tutorials, which usualy goes something like: "Rolling two six-sided dice produces results between 2 and 12. What are the chances of rolling each possible result?"
I coded that as a one-liner:
$ perl -e 'my ($n, $s, $r) = @ARGV; my %x; for (1 .. $r) { my $y = 0; +for (1 .. $n) { $y += 1 + int rand $s } ++$x{$y} } printf "Chance of +rolling %3d is %3d%%\n", $_, int $x{$_} / $r * 100 for sort { $a <=> +$b } keys %x' 2 6 1000000
I've broken that up into multiple lines for ease of viewing (and added the results of a sample run):
$ perl -e ' my ($n, $s, $r) = @ARGV; my %x; for (1 .. $r) { my $y = 0; for (1 .. $n) { $y += 1 + int rand $s } ++$x{$y} } printf "Chance of rolling %3d is %3d%%\n", $_, int $x{$_} / $r * 100 for sort { $a <=> $b } keys %x ' 2 6 1000000 Chance of rolling 2 is 2% Chance of rolling 3 is 5% Chance of rolling 4 is 8% Chance of rolling 5 is 11% Chance of rolling 6 is 13% Chance of rolling 7 is 16% Chance of rolling 8 is 13% Chance of rolling 9 is 11% Chance of rolling 10 is 8% Chance of rolling 11 is 5% Chance of rolling 12 is 2%
So, if you think of your question in terms of rolling five 50-sided dice, this might be the sort of thing you're after; there again, it might be nothing like what you want.
The code I provided would not, I imagine, be acceptable for a class project: variable names should be meaningful; statements should be formally terminated with semicolons; and the final statement would be better as an actual block. There's no validation of input or error checking (for example, because all percentages are rounded down to the nearest integer, their sum is only 94%, not 100%). You may also have coding standards which you need to follow.
I'd recommend you fully understand the code before enhancing it. Although the single-letter variable names made sense to me when I was writing the one-liner, they may be as clear as mud to you or others: $n is the number of dice; $s is the number of sides; $r is the number of rolls; %x is a hash which collects the results; and, $y is a temporary value which is reinitialised on each iteration of the outer loop. See also sprintf.
— Ken
In reply to Re: Dice roll chances (was: Write ... line)
by kcott
in thread Dice roll chances
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |