Repository navigation
Expand file tree
/
Copy pathalgC4.c
More file actions
110 lines (100 loc) · 2.34 KB
/
Copy pathalgC4.c
File metadata and controls
110 lines (100 loc) · 2.34 KB
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
/******************************************************
Robert Jones
Feb 2, 1997
CSci 726
Algorithm C.4 gap test
for Exercise C.7
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "rvms.h"
#include "rngs.h"
#define n 1000L
#define a 0.93
#define b 0.96
#define alpha 0.05
double log(double x);
double exp(double x);
int main(void)
{
long x, i, k; /* indices */
long rns; /* rng stream */
long *o; /* histogram */
long num_below = 0;
long num_above = 0;
double u; /* ~Unif(0,1) sample */
double e; /* expected number of gaps */
double v, v1, v2;
double diff;
const double delta = b - a;
const double ndelta = n*delta;
/*
* compute k
*/
k = 1 + (long)(log(10.0/ndelta)/log(1.0-delta));
if (k < 1) {
fprintf(stderr, "Error: histogram size must be positive.\n");
return EXIT_FAILURE;
}
/*
* allocate memory for histogram
*/
o = (long *)calloc(k+1, sizeof(long));
/*
* determine critical values v1 and v2
*/
v1 = idfChisquare(k,alpha/2);
v2 = idfChisquare(k,1-alpha/2);
printf("\nk = %ld, v1 = %g, v2 = %g\n",k,v1,v2);
PlantSeeds(231886580);
/* for each random number stream s */
for (rns = 0; rns < 256; rns++) {
SelectStream(rns);
/*
* compute histogram
*/
for (x = 0; x <= k; x++) {
o[x] = 0;
}
for (i = 1; i <= n; i++) {
x = -1;
do {
u = Random();
x += 1;
} while (u < a || u > b);
if (x > k) {
x = k;
}
o[x] += 1;
if (0) printf("x=%ld\n",x);
}
/*
* compute chi-square statistic
*/
v = 0.0;
for (x = 0; x < k; x++) {
e = ndelta*pow(1-delta,x);
diff = o[x] - e;
v += diff*diff/e;
}
/* x = k */
e = n*pow(1-delta,x);
diff = o[x] - e;
v += diff*diff/e;
/*
* pass or fail?
*/
if (v < v1) {
num_below++;
}
else if (v > v2) {
num_above++;
}
}/* end for each stream */
/*
* the test results...
*/
printf("\nOf the %d tests, %ld failed with probability %g,\n",256,num_below+num_above,1-alpha);
printf(" %ld were less than v1 and %ld were greater than v2.\n",num_below,num_above);
}/*end*/