"Monte Carlo" Perl

This is pretty cool - had not seen it before.

http://math.fullerton.edu/mathews/n2003/MonteCarloPiMod.html

http://en.wikipedia.org/wiki/Monte_Carlo_method

Those of you with cygwin or unix and perl:

#!/usr/bin/perl
my $COUNT = 8; #Orders of magnitude
my $HIT = 0;

for(my $i = 1; $i < 10**$COUNT; ++$i){
        $HIT++ if rand()**2 + rand()**2 <= 1;
        printf "PI ~ %1.8f after %0${COUNT}i points\n", 4*$HIT/$i, $i if
               ($i % 1_000) == 0;
}

I got PI ~ 3.14155046 after 99999000 points and a second run with PI ~ 3.14160204 after 999990000 points

Comments

No comments.