Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 chilling in the Monastery: (4)
    As of 2014-12-19 06:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (71 votes), past polls