U E D R , A S I H C RSS

몬테카를로법/pi구하기

몬테카를로 법을 이용하여 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);
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:28
Processing time 0.0171 sec