23 # 24 # pi*($d/2)**2 in circle 25 # ------------ ~ ----------- ~ pi/4 26 # $d**2 in total 27 # 28 29 30 31 use strict; 32 use warnings; 33 34 my $d = 1000;#diameter 35 my $no_total = 1000000;#number of points 36 my $no_in=0; 37 38 sub gen_xy { ( int(rand($d)),int(rand($d)) ); } 39 40 sub in_circle { 41 my ($x,$y) = @_; 42 ($x - $d/2)**2 + ($y - $d/2)**2 <= ($d/2)**2 43 ? 1 44 : 0; 45 } 46 47 48 $no_in += in_circle gen_xy 49 for 1..$no_total; 50 51 warn "Pi approx: ". ( $no_in/$no_total ) *4 ;