#!/usr/bin/perl

#
# Finding the area of an ellipse using the Monte Carlo technique
#

$t = 10000; $c = 0;			# Variables
$xSize = 3; $ySize = 4;			# Size of bounding box (0 to end)

for($i = 0; $i < $t; $i++) {
  $x = rand(2 * $xSize) - $xSize;	# Choose random number on x-
  $y = rand(2 * $ySize) - $ySize;	#  and y-axis to represent dot
  if(($x**2)/9 + ($y**2)/16 <= 1) {	# If it lies in the ellipse
    $c++;				#  add one to counter
  }
}

print "Probability: $c out of $t trials.\n";

$p = $c / $t;
print "Probability of ellipse in bounding box: $p\n";

$q = 4 * $xSize * $ySize;
print "Area of bounding box: $q units\n";

$a = ($c / $t) * (6 * 8);
print "Approx. area of ellipse: $a units\n";
