Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- unsigned long int moje_rand(void)
- {
- static unsigned long int iterace = 1;
- const unsigned long int a = 279470273;
- const unsigned long int b = 0;
- const unsigned long int c = 4294967291;
- iterace = (a * iterace + b) % c;
- return iterace;
- }
- unsigned long int moje_rand2(void)
- {
- static unsigned long int iterace = 7777;
- const unsigned long int a = 279470273;
- const unsigned long int b = 0;
- const unsigned long int c = 4294967291;
- iterace = (a * iterace + b) % c;
- return iterace;
- }
- void histo(double *value, int samples, int *bin, int num_bins)
- {
- for (int i = 0; i < num_bins; i++)
- bin[i] = 0;
- for (int i = 0; i < samples; i++)
- {
- int bin_index = (int)((value[i] + 6.0) * num_bins / 12.0);
- if (bin_index >= 0 && bin_index < num_bins)
- bin[bin_index]++;
- }
- }
- double zvon() {
- double R1 = (double)moje_rand() / 4294967291.0;
- double R2 = (double)moje_rand2() / 4294967291.0;
- double x = sqrt(-2 * log(R1)) * cos(2 * M_PI * R2);
- return x;
- }
- int main()
- {
- double *values;
- int N = 10000;
- int *histogram;
- int pocet_slotu = 20;
- int i;
- values = (double *)malloc(N * sizeof(double));
- histogram = (int *)malloc(pocet_slotu * sizeof(int));
- for (i = 0; i < N; i++)
- {
- values[i] = zvon();
- }
- histo(values, N, histogram, pocet_slotu);
- double bin_width = 12.0 / pocet_slotu;
- for (i = 0; i < pocet_slotu; i++)
- {
- double lower_bound = -6.0 + i * bin_width;
- double upper_bound = -6.0 + (i + 1) * bin_width;
- printf("[%lf-%lf] - %d\n", lower_bound, upper_bound, histogram[i]);
- }
- free(histogram);
- free(values);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement