1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright ©️ 2024 Mario Forzanini <mf@marioforzanini.com>
*
* 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
* <https://www.gnu.org/licenses/>.
*
*/
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
|