#include "RandomSource.h" #include // 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; rgetRandomBoundedInt( 0, 1 ); //drawnNumbers[i] = inSource->getRandomBoolean(); for( int b=0; b> 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 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; }