in reply to Monte Carlo - Coin Toss
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Monte Carlo - Coin Toss
by eye (Chaplain) on Mar 12, 2011 at 16:41 UTC | |
|
Re^2: Monte Carlo - Coin Toss
by BrowserUk (Patriarch) on Mar 12, 2011 at 14:38 UTC | |
by roboticus (Chancellor) on Mar 12, 2011 at 16:11 UTC | |
by repellent (Priest) on Mar 13, 2011 at 04:31 UTC | |
by roboticus (Chancellor) on Mar 13, 2011 at 05:45 UTC | |
by repellent (Priest) on Mar 13, 2011 at 06:01 UTC |