James_H:
Obviously your "modest laptop" is a bit zippier than my Atom-based desktop, as it took 3:10 to complete. But when I looked at your code, I was wondering why you did a monte-carlo simulation. Since there are only 20 coins, there are a little over a million combinations, so why not compute it exactly?
So I whipped this up:
$ cat 892293.pl
use strict;
use warnings;
my $numTosses = 20; # Coin tosses per experiment (20 in this example)
my $runs = (1<<$numTosses)-1;
my @tailCnt;
for (my $collection=0; $collection<1<<$numTosses; ++$collection) {
my $tails=0;
$tails += ($collection & 1<<$_) ? 1 : 0 for 0 .. $numTosses;
$tailCnt[$tails]++;
}
print <<EOHDR;
Tails Count %
----- -------- ------
EOHDR
for (my $i = 0; $i < $numTosses+1; $i++) {
printf "% 4u % 8u %5.2f\n",
$i, $tailCnt[$i], 100*$tailCnt[$i]/$runs;
}
$ time perl 892293.pl
0x00100000, 1048576
Tails Count %
----- -------- ------
0 1 0.00
1 20 0.00
2 190 0.02
3 1140 0.11
4 4845 0.46
5 15504 1.48
6 38760 3.70
7 77520 7.39
8 125970 12.01
9 167960 16.02
10 184756 17.62
11 167960 16.02
12 125970 12.01
13 77520 7.39
14 38760 3.70
15 15504 1.48
16 4845 0.46
17 1140 0.11
18 190 0.02
19 20 0.00
20 1 0.00
real 0m20.372s
user 0m20.273s
sys 0m0.016s
It runs quite a bit faster on my machine, and it tried each combination once. Of course, I'm limited by the number of bits available, so large experiments aren't going to work well. You can use Bit::Vector, but I suspect that would be a good deal slower. (I wouldn't know, I didn't try it.)
...roboticus
When your only tool is a hammer, all problems look like your thumb.
-
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.
|