File: testRandom.cpp

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (215 lines) | stat: -rw-r--r-- 4,875 bytes parent folder | download | duplicates (15)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "RandomSource.h"

#include <stdio.h>


// perform tests on a random source

// tests found here:
// NIST FIPS 140-1 U.S. Federal Standard
// http://csrc.nist.gov/publications/fips/fips140-1/fips1401.pdf

void testRandom( RandomSource *inSource ) {
    
    int bitsPerStep = 1;
    

    // 20000 bits
    // must be a multiple of bitsPerStep
    int numBits = 20000;
    
    int numSteps = numBits / bitsPerStep;

    unsigned int *drawnNumbers = new unsigned int[ numSteps ];
    
    unsigned int *drawnBits = new unsigned int[ numBits ];
    

    unsigned int bitSum = 0;
    
    // 1 or 0 when we're in a run
    unsigned int currentRunType = 2;
    unsigned int currentRunLength = 0;
    

    unsigned int longestRunLength = 0;
    
    #define maxRunLengthToTrack 100

    unsigned int runLengthCounts[2][ maxRunLengthToTrack ];
        
    int r;
    for( r=0; r<maxRunLengthToTrack; r++ ) {
        runLengthCounts[0][r] = 0;
        runLengthCounts[1][r] = 0;
        }
    

    int i;
    int bitIndex = 0;
    
    for( i=0; i<numSteps; i++ ) {
        drawnNumbers[i] = inSource->getRandomBoundedInt( 0, 1 );
        //drawnNumbers[i] = inSource->getRandomBoolean();


        for( int b=0; b<bitsPerStep; b++ ) {
            unsigned int bit = ( drawnNumbers[i] >> b & 0x01 );
            bitSum += bit;
            
            drawnBits[bitIndex] = bit;
            bitIndex++;
            

            if( bit == currentRunType ) {
                currentRunLength++;
                

                if( currentRunLength > longestRunLength ) {
                    longestRunLength = currentRunLength;
                    }
                }
            else {
                // end of run

                if( currentRunLength > 0 && 
                    currentRunLength < maxRunLengthToTrack ) {
                    // count it
                    runLengthCounts[currentRunType][ currentRunLength ] ++;
                    }
                
                currentRunType = bit;
                currentRunLength = 1;
                }
            }
        }

    float mean = (float)bitSum / numBits;

    printf( "Mean = %f\n", mean );

    
    float varSum = 0;

    for( i=0; i<numBits; i++ ) {
        unsigned int bit = drawnBits[i];
        
        varSum += (bit - mean) * (bit - mean);
        }


    float variance = varSum / numBits;
    
    printf( "Var = %f\n", variance );

    printf( "Monobit count = %d  (Allowed [9654 .. 10346])\n", bitSum );
    
    printf( "Longest run = %d\n", longestRunLength );
    
    printf( "0 Run Length\tTimes Seen\n"
            "------------\t----------\n" );
    
    for( r=0; r<maxRunLengthToTrack && r <= longestRunLength; r++ ) {
        printf( "%d\t\t%d\n", r, runLengthCounts[0][r] );
        }
    printf( "1 Run Length\tTimes Seen\n"
            "------------\t----------\n" );
    
    for( r=0; r<maxRunLengthToTrack && r <= longestRunLength; r++ ) {
        printf( "%d\t\t%d\n", r, runLengthCounts[1][r] );
        }


    // poker test
    int j;
    int numCValues = numBits / 4;
    
    unsigned int *c = new unsigned int[numCValues];
    unsigned int *f = new unsigned int[16];
    
    for( i=0; i<16; i++ ) {
        f[i] = 0;
        }
    
    
    for( j=1; j<numCValues; j++ ) {
        //printf( "bit[%d] = %d\n", j, drawnBits[j] );
        
        c[j] = 
            8 * drawnBits[ 4 * j - 3]
            +
            4 * drawnBits[ 4 * j - 2]
            +
            2 * drawnBits[ 4 * j - 1]
            +
            1 * drawnBits[ 4 * j ];

        f[ c[j] ] ++;
        }
    
    int fSum = 0;
    //printf( "Poker hand counts:\n" );
    for( i=0; i<16; i++ ) {
        //printf( "f[%d] = %d\n", i, f[i] );
        
        fSum += f[i] * f[i];
        }
    
    float Y = 16.0f/numCValues * fSum - numCValues;
    
    printf( "Poker test = %f   (Allowed [1.03 .. 57.4])\n", Y );
    
    delete [] c;
    delete [] f;
    
    

    // autocorrelation
    char failed = false;
    
    int numShifts = 5000;
    unsigned int *Z = new unsigned int[ numShifts ];
    
    for( int t=1; t<numShifts; t++ ) {
        Z[t] = 0;
        

        for( int j=0; j<5000; j++ ) {
            Z[t] += drawnBits[j] ^ drawnBits[ j + t ];
            }

        if( Z[t] > 2326 && Z[t] < 2674 ) {
            }
        else {
            failed = true;
            //printf( "autocorrelation test failed for Z[%d] = %d\n", t, Z[t] );
            }        
        }
    if( !failed ) {
        printf( "Autocorrelation test passed.\n" );
        }
    
    


    delete [] drawnNumbers;
    delete [] drawnBits;
    }




#include "CustomRandomSource.h"
#include "StdRandomSource.h"

int main() {
    
    CustomRandomSource randSource( 11234258 );
    //StdRandomSource randSource( 11 );
    
    testRandom( &randSource );
    

    return 0;
    }