/* * Copyright ©️ 2024 Mario Forzanini * * This file is part of my exercises for LSN. * * My exercises for LSN are free software: you can redistribute them * and/or modify them under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * My exercises for LSN are distributed in the hope that they will be * useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with my exercises for LSN. If not, see * . * */ #include #include #include #include #include #include "random.h" #include "std.h" #define M 100 #define NTHROWS 10000 #define PROBABILITY ((double)(NTHROWS) / (double)(M)) /*! Array where progressive values of χ2 are stored */ static double bins[M] = { 0 }; int main(void) { FILE *primes, *input; Random rnd = { 0 }; if (!(primes = fopen("Primes", "r"))) { STD_ERROR("Error opening Primes: %s\n", strerror(errno)); return 1; } else if (!(input = fopen("seed.in", "r"))) { STD_ERROR("Error opening seed.in: %s\n", strerror(errno)); return 1; } if (!random_init(&rnd, primes, input)) { return 1; } for (int j = 0; j < 100; j++) { double chi2 = 0.0; for (int i = 0; i < NTHROWS; i++) { double random = random_rannyu(&rnd); bins[(int)(floor(random * M))]++; } for (int bin = 0; bin < M; bin++) { chi2 += pow(bins[bin] - PROBABILITY, 2); } printf("%d %lf\n", j, chi2 / PROBABILITY); memset(bins, 0, LEN(bins) * sizeof(*bins)); } random_save_seed(&rnd, "seed.out"); return 0; }