http://www.perlmonks.org?node_id=58619

   1: #!/usr/bin/perl
   2: # This is a quick program to calculate pi using the Monte Carlo method.
   3: # I recomend inputting  a value for $cycles greater that 1000. 
   4: # I am working on a detailed explanation of how and why this works. 
   5: # I will add it as soon as I'm done.
   6: use strict;
   7: my ($cycles, $i, $yespi, $pi) = 0;
   8: srand;
   9: #print "Please enter number of cycles\n";
  10: print "Please enter the amount of cycles:";
  11: chomp($cycles = <STDIN>);
  12: while ($i < $cycles) {
  13:     my ($x, $y, $cdnt) = 0;
  14:     $x = rand;
  15:     $y = rand;
  16:     $cdnt = $x**2 + $y**2;
  17:     if ($cdnt <= 1) {
  18: 	++$yespi;
  19: }    
  20: ++$i;
  21: }
  22: $pi = ($yespi / $cycles) * 4;
  23: print "Pi is $pi\n";