1. Definition:
Monte Carlo technique is generally used to intergrate functions
using random numbers. Here is the method:
2. Examples in C language:
//1.Integral:
//-------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define pi 3.1416
int main()
{
float a ;
float b ;
int number;
printf("n Enter two limit numbers and a long integer : --> ");
scanf("%f %f %d", &a,&b,&number);
float n = 1.0/number;
float y;
float random_x;
float random_y;
int count=0;
int i = 0;
for (i =1; i <= number; i++)
{
unsigned int seed = (unsigned int)time(NULL);
srand (i*seed);
random_x = a + (rand()/(RAND_MAX +1.0))*(b - a);
random_y = a + (rand()/(RAND_MAX +1.0))*(3.0 + 1.0);
// choose a function
//------------------
y = 2*random_x;
if(y >= random_y )
{
count =count + 1;
}
}
printf("The rate is %d n",count);
printf("n");
float m;
m = count*n;
float s = m*8;
printf("The rate is %2.3fn",m);
printf("The area is %4.3fn",s);
return 0;
}
//2. Other function sin(x)
//-------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define pi 3.1416
int main()
{
float a ;
float b ;
int number;
float surface;
printf("n Enter two limit numbers, a long integer, and the surface : --> ");
scanf("%f %f %d %f", &a,&b,&number,&surface);
//In this example, the surface = pi
float n = 1.0/number;
float y;
float random_x;
float random_y;
int count=0;
int i = 0;
for (i =1; i <= number; i++)
{
unsigned int seed = (unsigned int)time(NULL);
srand (i*seed);
random_x = a + (rand()/(RAND_MAX +1.0))*(b - a);
random_y = a + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0);
y = sin(random_x);
//chosen function : sin(x)
//-------------------------
if(y >= random_y )
{
count = count + 1;
}
}
printf("The rate is %d n",count);
printf("n");
float m;
m = count*n;
float s = m*surface;
printf("The rate is %2.3fn",m);
printf("The area is %4.3fn",s);
return 0;
}
//3. Calculating the number pi:
//-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
int number;
printf("n Enter a long integer : --> n");
scanf("%d" , &number);
float n = 1.0/number;
float yo;
float y;
float random_x;
float random_y;
int count=0;
int i = 0;
for (i =1; i <= number; i++)
{
unsigned int seed = (unsigned int)time(NULL);
srand (i*seed);
//generating random numbers between 0 and 1
random_x = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0);
random_y = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0);
yo = pow(random_x,2) + pow(random_y,2);
y = sqrt(yo);
if(y < = 1)/*condition to be within the circle: the distance y must be
lower or egal to the radius if of the circle which is equal to 1. */
{
count = count + 1;
}
}
printf("The hit count is %d n", count);
printf("n");
float m;
m = count*n;
float surface = 1;
// = 1 x 1 , the surface of the square
float s = m*surface;
printf("The rate count/number is %2.3fn",m);
printf("n");
printf("The value of pi/4 is %4.3fn",s);
printf("n");
printf("The value of pi is %4.3fn",4*s);
return 0;
}
//4. Using Gaussain distribution to calculate a probability
// -------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define pi 3.1416
int main()
{
float Po = 2*sqrt(pi);
float P = 1.0/Po;
float a ;
float b ;
int number;
printf("n Enter two limit numbers, and a long integer : --> ");
scanf("%f %f %d", &a,&b,&number);
float n = 1.0/number;
float y;
float random_x;
float random_y;
int count=0;
int i = 0;
for (i =1; i<= number; i++)
{
unsigned int seed = (unsigned int)time(NULL);
srand (i*seed);
random_x = a + (rand()/(RAND_MAX +1.0))*(b - a);
random_y = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0);
float w = 0.5*pow(random_x,2);
y = exp (-w);
if(y >= random_y )
{
count = count + 1;
}
}
printf("The hit count is %d n",count);
printf("n");
float m;
float surface;
surface = 1*(b-a);
m = count*n;
float s = P*m*surface ;
printf("The rate count/number is %6.6fn",m);
printf("The area is %4.3fn",s);
return 0;
}
/*
Notes:
--------
1.
count = ∑ f(random_x)
(i: from 1 to number)
the function f is the hit-count function
P = 1/Po = 1/sqrt(2*pi) for normalisation
f = P*count
s P*m*(b-a) = P*count*n*(b-a)= P*count*(b-a)/number
Thus the area is:
s = [(b-a)/number] * ∑ f(random_x)
(i: from 1 to number)
2.
Here, the area is the probability to find an event between a and b,
using Gauss probability distribution.
*/