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
|
/*
* 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 <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#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;
}
|