A simple program to computing Pi using the Monte Carlo method:


#include “stdlib.h”

#include “stdio.h”

#include “time.h”

int isinside(int , int);

int main()



        double pi;

        int x,y;

        int i=0;

        int inside=0;

        int n=10000;









        printf(“pi = %lf\n”,inside*4.0/n);


int isinside(int x,int y)






        if((x==5 && y==0) || (x==4 && y<4) || (x==3 && y<5) || (x==2 && y<5) || (x==1 && y<5) || x==0 )

                return 1;


                return 0;


bramani@chassis01:/home/bramani/toyprojs> cc pi.c

bramani@chassis01:/home/bramani/toyprojs> a.out


pi = 2.664800 [oops]

bramani@chassis01:/home/bramani/toyprojs> a.out


pi = 2.724800 [aiyo]


These pi values are for r=5.

As r increases pi tends to 3.14. 



I loved this experiment and thought of sharing.
Inspired by Fooled by randomness ~ Nassim Nicholas Taleb.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: