#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define LIMIT 100000000 //max value of n void main(){ double mypi,x,y ; int minus ; // 1 for pos., 2 for neg.
long int m,n;
time_t t;
n = 0; m = 0;
srand((unsigned) time(&t));
clrscr();
do{
n++; x = (rand()/(double)RAND_MAX); //pick a random X between 0..1
minus = 1+(rand()/(double)RAND_MAX);//pick a sign for X
switch (minus) {
case1 : x = x; case2 : x = -1*x; }y = rand()/(double)RAND_MAX;//pick a random Y between 0..1
minus = 1+rand()/(double)RAND_MAX;//pick a sign for Y
switch (minus) {
case1 : x = x; case2 : x = -1*x; }if (pow(x,2)+pow(y,2) < 1) m++; //the point is inside the unit circle mypi = 4.0*m/n; //calculate pi if (n % (LIMIT / 1000) == 0) {
gotoxy(1,1);
printf("X:%f ",x);
printf("Y:%f ",y);
printf("n:%ld ",n);
printf("m:%ld ",m);
printf(" - ");
printf("PI:%f ",mypi);
}
}
while (n != LIMIT); getch();
}