Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://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":



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found