## Posted By

DavidAspden on 09/28/18

# Estimate Pi

/ Published in: Python

So we use the fact that given a quarter circle of radius 1 we can generate random numbers and test whether they are inside the circle or outside. The probability that they are inside is pi/4 so we can run a large number of trials to get an approximation to Pi.

`#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Sep 27 16:52:34 2018""" ######Estimate pi ###import the mathsfrom math import *from random import * ###Pi Calculating Function###n is the number of trialsdef pimaker(n):    #Our counter is initialised    ins=0     for i in range (n):        #random number generation        x=random()        y=random()         ##calc and test the inequality        if sqrt(x**2 + y**2)<1:            #inside, so increment the counter            ins=ins+1    #Now just pretty print the estimate doing inline calculation.        print("Our estimate of pi is ", 4*(ins/n)) #Test The Function with various precision as below. #pimaker(5)#pimaker(10000000)`