-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 2: update section with Monte Carlo algorithm and a fix
- Loading branch information
Showing
3 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(int argc, char* argv[]) { | ||
double x,y,z,count; | ||
|
||
int seed = atoi(argv[1]); // Seed for srand | ||
int n = atoi(argv[2]); // Number of iterations | ||
|
||
srand(seed); | ||
|
||
for (int i = 0; i < n; i++) { | ||
x = (double) rand() / RAND_MAX; | ||
y = (double) rand() / RAND_MAX; | ||
z = x * x + y * y; | ||
if (z <= 1) count++; | ||
} | ||
|
||
printf("π approx.: %g", (count / n) * 4); | ||
|
||
return(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters