What stopped you from adapting the program I posted? Anyway:
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use List::Util 'sum';
my @dice = ([1 => 0.05], [5 => 0.1], [7 => 0.2], [13 => 0.3], [17 =>
+ 0.35]);
my %dice = map {$$_[0], $$_[1]} @dice;
my $THROWS = 5;
my %cache;
sub chance;
sub chance {
my $throws = shift;
my $target = shift;
return $cache{$throws, $target} if defined $cache{$throws, $target
+};
return $cache{$throws, $target} = 0 if $throws < 1 || $target < $t
+hrows * $dice[0][0] ||
$target > $t
+hrows * $dice[-1][0];
return $cache{$throws, $target} = $dice{$target} || 0 if $throws =
+= 1;
return $cache{$throws, $target} =
sum map {$dice{$_} * chance $throws-1, $target-$_} keys %d
+ice;
}
my $start = sum times;
my %total;
my $MIN = $THROWS * $dice[0][0];
my $MAX = $THROWS * $dice[-1][0] + 1;
foreach my $value ($MIN .. $MAX) {
my $chance = chance $THROWS, $value;
foreach my $value ($MIN .. $value) {
$total{$value} += $chance;
}
}
foreach my $value ($MIN .. $MAX) {
say "Chance of rolling $value or higher: $total{$value}";
}
my $end = sum times;
say "Running time: ", 1000 * ($end - $start), " milli-seconds";
__END__
Chance of rolling 5 or higher: 1
Chance of rolling 6 or higher: 0.9999996875
Chance of rolling 7 or higher: 0.9999996875
Chance of rolling 8 or higher: 0.9999996875
Chance of rolling 9 or higher: 0.9999996875
Chance of rolling 10 or higher: 0.9999965625
Chance of rolling 11 or higher: 0.9999965625
Chance of rolling 12 or higher: 0.9999903125
Chance of rolling 13 or higher: 0.9999903125
Chance of rolling 14 or higher: 0.9999778125
Chance of rolling 15 or higher: 0.9999778125
Chance of rolling 16 or higher: 0.9999278125
Chance of rolling 17 or higher: 0.9999278125
Chance of rolling 18 or higher: 0.9998434375
Chance of rolling 19 or higher: 0.9998434375
Chance of rolling 20 or higher: 0.9996934375
Chance of rolling 21 or higher: 0.9996934375
Chance of rolling 22 or higher: 0.9992825
Chance of rolling 23 or higher: 0.9992825
Chance of rolling 24 or higher: 0.9987325
Chance of rolling 25 or higher: 0.9987325
Chance of rolling 26 or higher: 0.99781
Chance of rolling 27 or higher: 0.99781
Chance of rolling 28 or higher: 0.995835
Chance of rolling 29 or higher: 0.995835
Chance of rolling 30 or higher: 0.99346
Chance of rolling 31 or higher: 0.99346
Chance of rolling 32 or higher: 0.98981
Chance of rolling 33 or higher: 0.98981
Chance of rolling 34 or higher: 0.9829225
Chance of rolling 35 or higher: 0.9829225
Chance of rolling 36 or higher: 0.9755525
Chance of rolling 37 or higher: 0.9755525
Chance of rolling 38 or higher: 0.964499375
Chance of rolling 39 or higher: 0.964499375
Chance of rolling 40 or higher: 0.946949375
Chance of rolling 41 or higher: 0.946949375
Chance of rolling 42 or higher: 0.929305625
Chance of rolling 43 or higher: 0.929305625
Chance of rolling 44 or higher: 0.903868125
Chance of rolling 45 or higher: 0.903868125
Chance of rolling 46 or higher: 0.868668125
Chance of rolling 47 or higher: 0.868668125
Chance of rolling 48 or higher: 0.836118125
Chance of rolling 49 or higher: 0.836118125
Chance of rolling 50 or higher: 0.787436875
Chance of rolling 51 or higher: 0.787436875
Chance of rolling 52 or higher: 0.733586875
Chance of rolling 53 or higher: 0.733586875
Chance of rolling 54 or higher: 0.684515
Chance of rolling 55 or higher: 0.684515
Chance of rolling 56 or higher: 0.614865
Chance of rolling 57 or higher: 0.614865
Chance of rolling 58 or higher: 0.5482525
Chance of rolling 59 or higher: 0.5482525
Chance of rolling 60 or higher: 0.4874775
Chance of rolling 61 or higher: 0.4874775
Chance of rolling 62 or higher: 0.4036525
Chance of rolling 63 or higher: 0.4036525
Chance of rolling 64 or higher: 0.3487025
Chance of rolling 65 or higher: 0.3487025
Chance of rolling 66 or higher: 0.283185
Chance of rolling 67 or higher: 0.283185
Chance of rolling 68 or higher: 0.217035
Chance of rolling 69 or higher: 0.217035
Chance of rolling 70 or higher: 0.1733834375
Chance of rolling 71 or higher: 0.1733834375
Chance of rolling 72 or higher: 0.1219334375
Chance of rolling 73 or higher: 0.1219334375
Chance of rolling 74 or higher: 0.0813553125
Chance of rolling 75 or higher: 0.0813553125
Chance of rolling 76 or higher: 0.0663490625
Chance of rolling 77 or higher: 0.0663490625
Chance of rolling 78 or higher: 0.0277615625
Chance of rolling 79 or higher: 0.0277615625
Chance of rolling 80 or higher: 0.0277615625
Chance of rolling 81 or higher: 0.0277615625
Chance of rolling 82 or higher: 0.0052521875
Chance of rolling 83 or higher: 0.0052521875
Chance of rolling 84 or higher: 0.0052521875
Chance of rolling 85 or higher: 0.0052521875
Chance of rolling 86 or higher: 0
Running time: 20 milli-seconds
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|