File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed
contents/monte_carlo_integration/code/c Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ void monte_carlo(int samples) {
13
13
int count = 0 ;
14
14
15
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 ;
16
+ double x = (double )rand () * radius / RAND_MAX ;
17
+ double y = (double )rand () * radius / RAND_MAX ;
18
18
19
19
if (in_circle (x , y , radius )) {
20
20
count += 1 ;
@@ -30,7 +30,10 @@ void monte_carlo(int samples) {
30
30
int main () {
31
31
srand (time (NULL ));
32
32
33
- monte_carlo (1000000 );
34
-
33
+ double estimate = monte_carlo (1000000 );
34
+
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
+
35
38
return 0 ;
36
39
}
You can’t perform that action at this time.
0 commit comments