1 /*
2 * $Id: sts_runs.c 237 2006-08-23 01:33:46Z rgb $
3 *
4 * See copyright in copyright.h and the accompanying file COPYING
5 * See also accompanying file STS.COPYING
6 *
7 */
8
9 /*
10 *========================================================================
11 * This is a the monobit test, rewritten from the description in the
12 * STS suite.
13 *
14 * Rewriting means that I can standardize the interface to gsl-encapsulated
15 * routines more easily. It also makes this my own code.
16 *========================================================================
17 */
18
19 #include <dieharder/libdieharder.h>
20
sts_runs(Test ** test,int irun)21 int sts_runs(Test **test, int irun)
22 {
23
24 int b,t;
25 uint value;
26 uint *rand_int;
27 Xtest ptest;
28 double pones,c00,c01,c10,c11;;
29
30 /*
31 * for display only. 2 means sts_runs tests 2-tuples.
32 */
33 test[0]->ntuple = 2;
34
35 /*
36 * Allocate the space needed by the test.
37 */
38 rand_int = (uint *)malloc(test[0]->tsamples*sizeof(uint));
39
40 /*
41 * Number of total bits from -t test[0]->tsamples = size of rand_int[]
42 */
43 bits = rmax_bits*test[0]->tsamples;
44
45 /*
46 * We have to initialize these a bit differently this time
47 */
48 ptest.x = 0.0;
49
50 /*
51 * Create entire bitstring to be tested
52 */
53 for(t=0;t<test[0]->tsamples;t++){
54 rand_int[t] = gsl_rng_get(rng);
55 }
56
57 /*
58 * Fill vector of "random" integers with selected generator.
59 * NOTE WELL: This can also be done by reading in a file!
60 */
61 pones = 0.0;
62 c00 = 0.0;
63 c01 = 0.0;
64 c10 = 0.0; /* Equal to c01 by obvious periodic symmetry */
65 c11 = 0.0;
66 for(b=0;b<bits;b++){
67 /*
68 * This gets the integer value of the ntuple at index position
69 * n in the current bitstring, from a window with cyclic wraparound.
70 */
71 value = get_bit_ntuple(rand_int,test[0]->tsamples,2,b);
72 switch(value){
73 case 0: /* 00 no new ones */
74 c00++;
75 break;
76 case 1: /* 01 no new ones */
77 c01++;
78 ptest.x++;
79 break;
80 case 2: /* 10 one new one (from the left) */
81 c10++;
82 ptest.x++;
83 pones++;
84 break;
85 case 3: /* 11 one new one (from the left) */
86 c11++;
87 pones++;
88 break;
89 }
90 MYDEBUG(D_STS_RUNS) {
91 printf("# sts_runs(): ptest.x = %f, pone = %f\n",ptest.x,pones);
92 }
93 }
94 /*
95 * form the probability of getting a one in the entire sample
96 */
97 pones /= (double) test[0]->tsamples*rmax_bits;
98 c00 /= (double) test[0]->tsamples*rmax_bits;
99 c01 /= (double) test[0]->tsamples*rmax_bits;
100 c10 /= (double) test[0]->tsamples*rmax_bits;
101 c11 /= (double) test[0]->tsamples*rmax_bits;
102
103 /*
104 * Now we can finally compute the targets for the problem.
105 */
106 ptest.y = 2.0*bits*pones*(1.0-pones);
107 ptest.sigma = 2.0*sqrt(bits)*pones*(1.0-pones);
108
109 MYDEBUG(D_STS_RUNS) {
110 printf(" p = %f c00 = %f c01 = %f c10 = %f c11 = %f\n",pones,c00,c01,c10,c11);
111 }
112
113 Xtest_eval(&ptest);
114 test[0]->pvalues[irun] = ptest.pvalue;
115
116 MYDEBUG(D_STS_RUNS) {
117 printf("# sts_runs(): test[0]->pvalues[%u] = %10.5f\n",irun,test[0]->pvalues[irun]);
118 }
119
120 free(rand_int);
121
122 return(0);
123
124 }
125
126