Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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.

I added mind numbing comments for utter beginners. Enjoy !

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!
  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (13)
    As of 2015-07-31 12:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (277 votes), past polls