We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 684475c commit 0e3e665Copy full SHA for 0e3e665
contents/monte_carlo_integration/code/c/monte_carlo.c
@@ -13,8 +13,8 @@ void monte_carlo(int samples) {
13
int count = 0;
14
15
for (int i = 0; i < samples; ++i) {
16
- double x = (double)rand() * 2.0 / RAND_MAX - 1.0;
17
- double y = (double)rand() * 2.0 / RAND_MAX - 1.0;
+ double x = (double)rand() * radius / RAND_MAX;
+ double y = (double)rand() * radius / RAND_MAX;
18
19
if (in_circle(x, y, radius)) {
20
count += 1;
@@ -30,7 +30,10 @@ void monte_carlo(int samples) {
30
int main() {
31
srand(time(NULL));
32
33
- monte_carlo(1000000);
34
-
+ double estimate = monte_carlo(1000000);
+
35
+ printf("The estimate of pi is %f\n", estimate);
36
+ printf("Percentage error: %0.2f%\n", 100 * fabs(M_PI - estimate) / M_PI);
37
38
return 0;
39
}
0 commit comments