/* * 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 NREALIZATIONS 10000 #define LAMBDA 1.0 #define MEAN 0.0 #define GAMMA 1.0 #define STANDARD_NAME "STANDARD" #define EXP_NAME "EXPONENTIAL" #define LORENTZ_NAME "LORENTZ" enum method { METHOD_UNKNOWN, METHOD_STD, METHOD_EXP, METHOD_LORENTZ, }; void usage(char *argv0) { fprintf(stderr, "USAGE: %s METHOD N\n" "\tMETHOD is one of " STANDARD_NAME ", " EXP_NAME ", " LORENTZ_NAME "\n", argv0); exit(1); } int main(int argc, char *argv[]) { enum method method = METHOD_UNKNOWN; long N = -1; 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; } if (argc < 3) { usage(argv[0]); } else { char *endptr = NULL; if (strcmp(argv[1], STANDARD_NAME) == 0) { method = METHOD_STD; } else if (strcmp(argv[1], EXP_NAME) == 0) { method = METHOD_EXP; } else if (strcmp(argv[1], LORENTZ_NAME) == 0) { method = METHOD_LORENTZ; } else { usage(argv[0]); } N = strtol(argv[2], &endptr, 10); if (N <= 0) { if (errno != 0) { STD_ERROR("Errors parsing N: %s\n", strerror(errno)); } else if (argv[2] == endptr) { STD_ERROR("Errors parsing N: Expected a " "number, got %s\n", argv[2]); } else { STD_ERROR("N must be a positive number%s", "\n"); } usage(argv[0]); } } double sum = 0.0; switch (method) { case METHOD_STD: { for (int i = 0; i < NREALIZATIONS; i++) { for (int n = 0; n < N; n++) { sum += random_rannyu(&rnd); } printf("%lf\n", sum / (double)N); sum = 0.0; } } break; case METHOD_EXP: { for (int i = 0; i < NREALIZATIONS; i++) { for (int n = 0; n < N; n++) { sum += random_exp(&rnd, LAMBDA); } printf("%lf\n", sum / (double)N); sum = 0.0; } } break; case METHOD_LORENTZ: { for (int i = 0; i < NREALIZATIONS; i++) { for (int n = 0; n < N; n++) { sum += random_lorentz(&rnd, MEAN, GAMMA); } printf("%lf\n", sum / (double)N); sum = 0.0; } } break; default: __builtin_unreachable(); } random_save_seed(&rnd, "seed.out"); return 0; }