Skip to content

Commit 9f8b4c8

Browse files
Gathrosberquist
authored andcommitted
Small change to monte_carlo.c (#501)
Removed radius and changed number of samples to be unsigned
1 parent 9133a55 commit 9f8b4c8

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

contents/monte_carlo_integration/code/c/monte_carlo.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
#include <stdlib.h>
55
#include <time.h>
66

7-
bool in_circle(double x, double y, double radius) {
8-
return x * x + y * y < radius * radius;
7+
bool in_circle(double x, double y) {
8+
return x * x + y * y < 1;
99
}
1010

11-
double monte_carlo(int samples) {
12-
double radius = 1.0;
13-
int count = 0;
11+
double monte_carlo(unsigned int samples) {
12+
unsigned int count = 0;
1413

15-
for (int i = 0; i < samples; ++i) {
16-
double x = (double)rand() * radius / RAND_MAX;
17-
double y = (double)rand() * radius / RAND_MAX;
14+
for (unsigned int i = 0; i < samples; ++i) {
15+
double x = (double)rand() / RAND_MAX;
16+
double y = (double)rand() / RAND_MAX;
1817

19-
if (in_circle(x, y, radius)) {
18+
if (in_circle(x, y)) {
2019
count += 1;
2120
}
2221
}
@@ -28,9 +27,9 @@ int main() {
2827
srand(time(NULL));
2928

3029
double estimate = monte_carlo(1000000);
31-
32-
printf("The estimate of pi is %f\n", estimate);
33-
printf("Percentage error: %0.2f%\n", 100 * fabs(M_PI - estimate) / M_PI);
34-
30+
31+
printf("The estimate of pi is %g\n", estimate);
32+
printf("Percentage error: %0.2f%%\n", 100 * fabs(M_PI - estimate) / M_PI);
33+
3534
return 0;
3635
}

0 commit comments

Comments
 (0)