Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define ARG_NUM 4
- double ran_cos(void);
- int main(int argc, char *argv[ARG_NUM]) {
- double r, d, cos_theta, cos_theta_crit, ratio, expected_ratio;
- int n;
- unsigned int counts, i;
- srand(time(NULL));
- if (argc != ARG_NUM) {
- fprintf(stderr, "Usage: %s <number of points> <radius> <distance>\n", argv[0]);
- return 1;
- }
- n = atoi(argv[1]);
- r = atof(argv[2]);
- d = atof(argv[3]);
- if (n <= 0) {
- fprintf(stderr, "Number of points must be a positive integer.\n");
- return 1;
- }
- if (r <= 0 || d <= 0) {
- fprintf(stderr, "Radius and distance must be positive numbers.\n");
- return 1;
- }
- cos_theta_crit = d / sqrt(r * r + d * d);
- printf("Critical cos(theta): %f\n", cos_theta_crit);
- for (i = 0, counts = 0; i < n; i++) {
- cos_theta = ran_cos();
- if (cos_theta > cos_theta_crit) {
- counts++;
- }
- }
- ratio = ((float)counts) / n;
- expected_ratio = 0.5 * (1 - cos_theta_crit);
- printf("Total points: %d\nTotal hits: %d\nRatio: %.6f\nExpected ratio: %.6f\n", n, counts, ratio, expected_ratio);
- return 0;
- }
- double ran_cos(void) {
- return 1 - 2 * ((double)rand() / RAND_MAX);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement