Pathologically Eclectic Rubbish Lister PerlMonks

### Comment on

 Need Help??

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.

In reply to Re: Monte Carlo - Coin Toss by roboticus
in thread Monte Carlo - Coin Toss by James_H

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-23 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (124 votes). Check out past polls.

Notices?