This program finds the value of ð (pi) with the Monte Carlo method.
#1Consiter that unit circle (radious = 1 ) within a square with sides equal to 2.If we pick a random point(x,y) were both x and y are
between -1,........1. the probability that this random point lies
inside the circle is given as the proportion:
P( x^2+y^2<1 ) = Area circle/Area square = ( pi )/4
#2If we pick a random point N times and M of those times the point lies inside the unit circle ,the probabillity of that a random point lies inside the unit circle is given as:
P ( x^2+y^2<1 ) = M/N
#3 But if N becomes very large (theoretically infinite)the true probabilities #1-#2 tend to became equal
4M/N=pi
WHY I GET THIS ERROR:undefined reference to `drand48(void)
Code:
#include<stdio.h>
#include<stdlib.h>
double x,y,pi,n_counter,count,m;
double drand48(void);
int main()
{
printf("Give N :\n");
scanf("%lf",&n_counter);
for(count=0;count<=n_counter;count++)
{
y=2.0*drand48()-1.0;
x=2.0*drand48()-1.0;
if(x*x+y*y<1)
{
m++;
}
}
pi=4.0*m/n_counter;
printf("Pi =%lf",pi);
return 0;
}