No such thing as a small change PerlMonks

Comment on

 Need Help??

I hope this is appropriate for this section

Recently, I was asked to find the probability density function of distribution (pdf) of tossing 20 coins at a time. The time it took me to write a Monte Carlo simulation was half the time it took me to compute it from the binomial dist or by building a Pascal's triangle. Maybe thats not a good reflection on my math, but as you will see, I'm not a fancy Perl programmer either :)

Anyway, here is the code which produces very accurate results in a matter of 30sec on my modest laptop.

Regards, James

```# coinToss.pl
#
# Toss a collection (20 in this example) of coins a large number of ti
+mes
# to determine the distribution of Heads and Tails.
# N.B. P(Head) = P(Tail) = 50% = 0.5

use strict;

# Inputs
my \$numTosses = 20;        # The num of coin tosses per experiment (20
+ in this example)
my \$runs = 10000000;    # 10 Million - the num of times we repeat the
+experiment

# Program vars
my \$i;            # a looping variable
my \$j;            # another looping var
my \$toss;        # Keeps running total of the number of 'Heads' during
+ current experiment
my @collect;    # An array that keeps a total of the number of 'Heads'
+ counted
# in all previous experiment.
my \$percent;    # To convert \$collect[0] - \$collect[19] to %

# Outer loop: Repeat "\$runs" times
for (\$j = 0; \$j < \$runs; \$j++) {
# Inner loop: One run of 20 tosses
for (\$i = 0; \$i < \$numTosses; \$i++) {
\$toss += (rand() < 0.5) ? 1 : 0;
#print "\$toss\n";
}
\$collect[\$toss]++;
\$toss = 0;
}

# Print results
print "\nTails\tCount out of \$runs\t%\n";
for (\$i = 0; \$i < \$numTosses+1; \$i++) {
\$percent = sprintf "%.2f", \$collect[\$i] / \$runs * 100;
print "\$i\t\$collect[\$i]\t\t\t\$percent%\n"
}

In reply to 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.
• 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?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-03-18 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (229 votes). Check out past polls.

Notices?