#!/usr/bin/python

import math, random

num_points=50000
origin=(0.0,0.0)

def random_point():
	# Return a random point from the first quadrant inside the
	# square with corners at (0,0) to (1,1).

	return (random.random(), random.random())

def distance(a,b):
	# Return the distance between points (a,b) in a Cartesian
	# plane.  (This should be generalized to n dimensions.)

	delta_x=b[0]-a[0]
	delta_y=b[1]-a[1]
	return math.sqrt(delta_x*delta_x+delta_y*delta_y)

def inside_curve(point):
	# Tell whether the point in question is inside (beneath?)
	# the curve we are trying to integrate.
	#
	# For a general Monte Carlo method to integrate any function f,
	# we would want something closer to
	#
	# return f(point[0])<point[1]
	#
	# In the unit circle case, f(x) = sqrt(1.0-x^2)
	#
	# This shortcut form of the calculation is cleaner, though:
	
	return (distance(origin,point)) < 1.0


inside_points = 0
for i in range(num_points):
	if (inside_curve(random_point())):
		inside_points = inside_points + 1

ratio = float(inside_points) / float(num_points)

# The bounding box for the whole circle has corners at (-1,-1) and (1,1)
# and therefore has area 4.
bounding_box_area = 4

inside_area = bounding_box_area * ratio

print inside_area
