1
/* Test file for the various mpfr_random fonctions.
3
Copyright 1999, 2000, 2001, 2002 Free Software Foundation.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
28
#include "mpfr-impl.h"
30
void test_random _PROTO ((unsigned long, unsigned long, int));
31
void test_random2 _PROTO ((unsigned long, unsigned long, int));
32
void test_urandomb _PROTO ((unsigned long, unsigned long, int));
35
test_random (unsigned long nbtests, unsigned long prec, int verbose)
38
int *tab, size_tab, k;
39
double d, av = 0, var = 0, chi2 = 0, th;
43
size_tab = (nbtests < 1000 ? nbtests / 50 : 20);
44
tab = (int *) malloc (size_tab * sizeof(int));
45
for (k = 0; k < size_tab; ++k) tab[k] = 0;
47
for (k = 0; k < nbtests; k++) {
49
d = mpfr_get_d1 (x); av += d; var += d*d;
50
tab[(int)(size_tab * d)]++;
54
if (!verbose) { free(tab); return; }
57
var = (var /nbtests) - av*av;
59
th = (double)nbtests / size_tab;
61
printf("Average = %.5f\nVariance = %.5f\n", av, var);
62
printf("Repartition for random. Each integer should be close to %d.\n",
65
for (k = 0; k < size_tab; k++) {
66
chi2 += (tab[k] - th) * (tab[k] - th) / th;
67
printf("%d ", tab[k]);
68
if (((k+1) & 7) == 0) printf("\n");
71
printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n\n",
81
test_random2 (unsigned long nbtests, unsigned long prec, int verbose)
84
int *tab, size_tab, k;
85
double d, av = 0, var = 0, chi2 = 0, th;
89
size_tab = (nbtests < 1000 ? nbtests / 50 : 20);
90
tab = (int *) malloc (size_tab * sizeof(int));
91
for (k = 0; k < size_tab; ++k) tab[k] = 0;
93
for (k = 0; k < nbtests; k++) {
94
mpfr_random2 (x, MPFR_ABSSIZE(x), 0);
95
d = mpfr_get_d1 (x); av += d; var += d*d;
97
tab[(int)(size_tab * d)]++;
101
if (!verbose) { free(tab); return; }
104
var = (var /nbtests) - av*av;
106
th = (double)nbtests / size_tab;
107
printf("Average = %.5f\nVariance = %.5f\n", av, var);
108
printf("Repartition for random2 (taking only values < 1 into account.\n");
110
for (k = 0; k < size_tab; k++) {
111
chi2 += (tab[k] - th) * (tab[k] - th) / th;
112
printf("%d ", tab[k]);
113
if (((k+1) & 7) == 0) printf("\n");
116
printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n\n",
124
test_urandomb (unsigned long nbtests, unsigned long prec, int verbose)
127
int *tab, size_tab, k;
128
gmp_randstate_t state;
129
double d, av = 0, var = 0, chi2 = 0, th;
133
size_tab = (nbtests < 1000 ? nbtests / 50 : 20);
134
tab = (int *) malloc (size_tab * sizeof(int));
135
for (k = 0; k < size_tab; ++k) tab[k] = 0;
137
gmp_randinit (state, GMP_RAND_ALG_LC, 128);
138
gmp_randseed_ui (state, time(NULL));
140
for (k = 0; k < nbtests; k++) {
141
mpfr_urandomb(x, state);
142
d = mpfr_get_d1 (x); av += d; var += d*d;
143
tab[(int)(size_tab * d)]++;
147
gmp_randclear(state);
148
if (!verbose) { free(tab); return; }
151
var = (var /nbtests) - av*av;
153
th = (double)nbtests / size_tab;
154
printf("Average = %.5f\nVariance = %.5f\n", av, var);
155
printf("Repartition for urandomb. Each integer should be close to %d.\n",
158
for (k = 0; k < size_tab; k++) {
159
chi2 += (tab[k] - th) * (tab[k] - th) / th;
160
printf("%d ", tab[k]);
161
if (((k+1) & 7) == 0) printf("\n");
164
printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n\n",
172
main (int argc, char *argv[])
174
unsigned long nbtests, prec; int verbose = 0;
176
if (argc > 1) verbose = 1;
178
if (argc == 1) { nbtests = 10000; } else nbtests = atoi(argv[1]);
179
if (argc <= 2) { prec = 1000; } else prec = atoi(argv[2]);
181
test_random(nbtests, prec, verbose);
182
test_random2(nbtests, prec, verbose);
183
test_urandomb(nbtests, prec, verbose);