[ create a new paste ] login | about

Link: http://codepad.org/BKdbnPIZ    [ raw code | output | fork ]

C, pasted on Dec 24:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define N 10000

int main(void)
{
	int i;
	int count = 0;
	
	for (i = 0; i < N; ++i) {
		double x = rand() / (double)RAND_MAX;
		double y = rand() / (double)RAND_MAX;
		double z = rand() / (double)RAND_MAX;
		if (x * x + y * y < (1 - z) * (1 - z)) {
			count++;
		}
	}
	
	printf("近似値:%f\n", count / (double)N);
	printf("理論値:%f\n", M_PI / 12);
	
	return 0;
}


Output:
1
2
近似値:0.259300
理論値:0.261799


Create a new paste based on this one


Comments: