Advertisement
Aurox_

geometric-acceptance

May 18th, 2025 (edited)
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | Science | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define ARG_NUM 4
  7.  
  8. double ran_cos(void);
  9.  
  10. int main(int argc, char *argv[ARG_NUM]) {
  11.     double r, d, cos_theta, cos_theta_crit, ratio, expected_ratio;
  12.     int n;
  13.     unsigned int counts, i;
  14.     srand(time(NULL));
  15.  
  16.     if (argc != ARG_NUM) {
  17.         fprintf(stderr, "Usage: %s <number of points> <radius> <distance>\n", argv[0]);
  18.         return 1;
  19.     }
  20.  
  21.     n = atoi(argv[1]);
  22.     r = atof(argv[2]);
  23.     d = atof(argv[3]);
  24.  
  25.     if (n <= 0) {
  26.         fprintf(stderr, "Number of points must be a positive integer.\n");
  27.         return 1;
  28.     }
  29.     if (r <= 0 || d <= 0) {
  30.         fprintf(stderr, "Radius and distance must be positive numbers.\n");
  31.         return 1;
  32.     }
  33.  
  34.    
  35.     cos_theta_crit = d / sqrt(r * r + d * d);
  36.     printf("Critical cos(theta): %f\n", cos_theta_crit);
  37.  
  38.     for (i = 0, counts = 0; i < n; i++) {
  39.         cos_theta = ran_cos();
  40.         if (cos_theta > cos_theta_crit) {
  41.             counts++;
  42.         }
  43.     }
  44.     ratio = ((float)counts) / n;
  45.     expected_ratio = 0.5 * (1 - cos_theta_crit);
  46.  
  47.     printf("Total points: %d\nTotal hits: %d\nRatio: %.6f\nExpected ratio: %.6f\n", n, counts, ratio, expected_ratio);
  48.  
  49.     return 0;
  50. }
  51.  
  52. double ran_cos(void) {
  53.     return 1 - 2 * ((double)rand() / RAND_MAX);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement