몬테카를로 법을 이용하여 pi 구하기 ¶
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <limits.h> #define TestCase_Num 1000000 int main() { double x, y, pi; int i, inner, Sum_Of_Inner=0; srand(time(NULL)); for(i=1; i<=TestCase_Num; i++){ x=1.0*rand()/RAND_MAX; y=1.0*rand()/RAND_MAX; if(x*x+y*y<=1) inner=1; else inner=0; Sum_Of_Inner=Sum_Of_Inner+inner; } pi=1.0*Sum_Of_Inner/TestCase_Num*4; printf("pi = %f\n", pi); return(0); }