Advertisement
Gaxil

Nz

May 2nd, 2024
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. unsigned long int moje_rand(void)
  6. {
  7.     static unsigned long int iterace = 1;
  8.     const unsigned long int a = 279470273;
  9.     const unsigned long int b = 0;
  10.     const unsigned long int c = 4294967291;
  11.  
  12.     iterace = (a * iterace + b) % c;
  13.     return iterace;
  14. }
  15.  
  16. unsigned long int moje_rand2(void)
  17. {
  18.     static unsigned long int iterace = 7777;
  19.     const unsigned long int a = 279470273;
  20.     const unsigned long int b = 0;
  21.     const unsigned long int c = 4294967291;
  22.  
  23.     iterace = (a * iterace + b) % c;
  24.     return iterace;
  25. }
  26.  
  27. void histo(double *value, int samples, int *bin, int num_bins)
  28. {
  29.     for (int i = 0; i < num_bins; i++)
  30.         bin[i] = 0;
  31.     for (int i = 0; i < samples; i++)
  32.     {
  33.         int bin_index = (int)((value[i] + 6.0) * num_bins / 12.0);
  34.  
  35.         if (bin_index >= 0 && bin_index < num_bins)
  36.             bin[bin_index]++;
  37.     }
  38. }
  39.  
  40. double zvon() {
  41.     double R1 = (double)moje_rand() / 4294967291.0;
  42.     double R2 = (double)moje_rand2() / 4294967291.0;
  43.  
  44.     double x = sqrt(-2 * log(R1)) * cos(2 * M_PI * R2);
  45.  
  46.     return x;
  47. }
  48.  
  49. int main()
  50. {
  51.     double *values;
  52.     int N = 10000;
  53.     int *histogram;
  54.     int pocet_slotu = 20;
  55.     int i;
  56.  
  57.     values = (double *)malloc(N * sizeof(double));
  58.     histogram = (int *)malloc(pocet_slotu * sizeof(int));
  59.  
  60.     for (i = 0; i < N; i++)
  61.     {
  62.         values[i] = zvon();
  63.     }
  64.  
  65.     histo(values, N, histogram, pocet_slotu);
  66.  
  67.     double bin_width = 12.0 / pocet_slotu;
  68.     for (i = 0; i < pocet_slotu; i++)
  69.     {
  70.         double lower_bound = -6.0 + i * bin_width;
  71.         double upper_bound = -6.0 + (i + 1) * bin_width;
  72.         printf("[%lf-%lf] - %d\n", lower_bound, upper_bound, histogram[i]);
  73.     }
  74.  
  75.     free(histogram);
  76.     free(values);
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement