- /********************************************************************************/
- /*
- /* Ce programme permet à l'aide de la méthode de Monte Carlo
- /* de déterminer de manière approximative pi
- /* Pour cela on trace un carré dans lequel figure un arc de cercle
- /* Puis l'on génère une série de points de coordonnées (x,y) dans ce carré
- /* Et à chaque fois le programme détermine si les points sont dans ou hors
- /* de l'arc de cercle à l'aide du calcul se réferrant à cette méthode puis enfin
- /* on fait le rapport du nombre de points dans le cercle (multiplié par 4)
- /* avec le nombre de points total ce qui au final permet la détermination
- /* approximative de pi.
- /*
- /********************************************************************************/
-
-
- /* Importation de bibliothèques */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- /* Cette fonction va permettre de générer des nombres aléatoires */
-
- int my_rand (void)
- {
- static int first = 0;
-
- if (first == 0)
- {
- srand (time (NULL));
- first = 1;
- }
- return (rand ());
- }
-
- /* Programme principal */
-
- int main()
- {
- printf(" ****************************\n");
- printf(" * *\n");
- printf(" * Programme de Monte Carlo *\n");
- printf(" * Cree par Shakan972 *\n");
- printf(" * Le 08/02/07 *\n");
- printf(" * *\n");
- printf(" ****************************\n\n");
-
- float x, y, pi;
- float n, i, pts_dans_cercle, pts_tot, t;
- i=0;
- pts_dans_cercle=0;
- pts_tot=0;
- printf("Veuillez saisir le nombre d'iterations a realiser : ");
- scanf("%f",&n);
- printf("\n");
- while (i<=n)
- {
- my_rand();
- x=rand()/(RAND_MAX+1.0); /* Génération de points de coordonnées aléatoires compris dans l'intervalle [0,1] */
- x=x*x;
- my_rand();
- y=rand()/(RAND_MAX+1.0);
- y=y*y;
- if (x+y<1)
- {
- pts_dans_cercle=pts_dans_cercle+1;
- }
- pts_tot=pts_tot+1;
- i=i+1;
- printf("Estimation de pi = %4f\n",(4*pts_dans_cercle)/(pts_tot));
- }
- printf("\n");
- printf("Estimation de pi termine !!\n\n");
- system("PAUSE");
- return 0;
- }
/********************************************************************************/
/*
/* Ce programme permet à l'aide de la méthode de Monte Carlo
/* de déterminer de manière approximative pi
/* Pour cela on trace un carré dans lequel figure un arc de cercle
/* Puis l'on génère une série de points de coordonnées (x,y) dans ce carré
/* Et à chaque fois le programme détermine si les points sont dans ou hors
/* de l'arc de cercle à l'aide du calcul se réferrant à cette méthode puis enfin
/* on fait le rapport du nombre de points dans le cercle (multiplié par 4)
/* avec le nombre de points total ce qui au final permet la détermination
/* approximative de pi.
/*
/********************************************************************************/
/* Importation de bibliothèques */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Cette fonction va permettre de générer des nombres aléatoires */
int my_rand (void)
{
static int first = 0;
if (first == 0)
{
srand (time (NULL));
first = 1;
}
return (rand ());
}
/* Programme principal */
int main()
{
printf(" ****************************\n");
printf(" * *\n");
printf(" * Programme de Monte Carlo *\n");
printf(" * Cree par Shakan972 *\n");
printf(" * Le 08/02/07 *\n");
printf(" * *\n");
printf(" ****************************\n\n");
float x, y, pi;
float n, i, pts_dans_cercle, pts_tot, t;
i=0;
pts_dans_cercle=0;
pts_tot=0;
printf("Veuillez saisir le nombre d'iterations a realiser : ");
scanf("%f",&n);
printf("\n");
while (i<=n)
{
my_rand();
x=rand()/(RAND_MAX+1.0); /* Génération de points de coordonnées aléatoires compris dans l'intervalle [0,1] */
x=x*x;
my_rand();
y=rand()/(RAND_MAX+1.0);
y=y*y;
if (x+y<1)
{
pts_dans_cercle=pts_dans_cercle+1;
}
pts_tot=pts_tot+1;
i=i+1;
printf("Estimation de pi = %4f\n",(4*pts_dans_cercle)/(pts_tot));
}
printf("\n");
printf("Estimation de pi termine !!\n\n");
system("PAUSE");
return 0;
}