I was trying to convert the NIST randomness tests to c++. I got this:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include "include\externs.h"
using namespace std;
void Frequency(double n)
{
int i;
double f, s_obs, p_value, sum, sqrt2 = 1.41421356237309504880;
sum = 0.0;
for ( i=0; i<n; i++ )
sum += 2*(int)epsilon[i]-1;
s_obs = fabs(sum)/sqrt(n);
f = s_obs/sqrt2;
p_value = erfc(f);
cout << "\t\t\t FREQUENCY TEST\n";
cout << "\t\t---------------------------------------------\n";
cout << "\t\tCOMPUTATIONAL INFORMATION:\n";
cout << "\t\t---------------------------------------------\n";
cout << "\t\t(a) The nth partial sum = " << (int)sum << "\n";
cout << "\t\t(B)/> S_n/n = " << sum/n << "\n";
cout << "\t\t---------------------------------------------\n";
string str = p_value < ALPHA ? "FAILURE" : "SUCCESS";
cout << str << "\t\tp_value = " << p_value << "\n\n";
cout << p_value << "\n";
}
I don't think I really knew what I was doing, but "erfc()" is not declared. After looking it up, I found it was some kind of error function. Where can I find a c++ implementation?
BTW, here is externs.h (NOTE: I haven't modified any of the header files):
#include "../include/defs.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L D A T A S T R U C T U R E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
extern BitSequence *epsilon; // BIT STREAM
extern TP tp; // TEST PARAMETER STRUCTURE
extern FILE *stats[NUMOFTESTS+1]; // FILE OUTPUT STREAM
extern FILE *results[NUMOFTESTS+1]; // FILE OUTPUT STREAM
extern FILE *freqfp; // FILE OUTPUT STREAM
extern FILE *summary; // FILE OUTPUT STREAM
extern int testVector[NUMOFTESTS+1];
extern char generatorDir[NUMOFGENERATORS][20];
extern char testNames[NUMOFTESTS+1][32];
and here is defs.h:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
D E B U G G I N G A I D E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "config.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
M A C R O S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define MAX(x,y) ((x) < (y) ? (y) : (x))
#define MIN(x,y) ((x) > (y) ? (y) : (x))
#define isNonPositive(x) ((x) <= 0.e0 ? 1 : 0)
#define isPositive(x) ((x) > 0.e0 ? 1 : 0)
#define isNegative(x) ((x) < 0.e0 ? 1 : 0)
#define isGreaterThanOne(x) ((x) > 1.e0 ? 1 : 0)
#define isZero(x) ((x) == 0.e0 ? 1 : 0)
#define isOne(x) ((x) == 1.e0 ? 1 : 0)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L C O N S T A N T S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define ALPHA 0.01 /* SIGNIFICANCE LEVEL */
#define MAXNUMOFTEMPLATES 148 /* APERIODIC TEMPLATES: 148=>temp_length=9 */
#define NUMOFTESTS 15 /* MAX TESTS DEFINED */
#define NUMOFGENERATORS 10 /* MAX PRNGs */
#define MAXFILESPERMITTEDFORPARTITION 148
#define TEST_FREQUENCY 1
#define TEST_BLOCK_FREQUENCY 2
#define TEST_CUSUM 3
#define TEST_RUNS 4
#define TEST_LONGEST_RUN 5
#define TEST_RANK 6
#define TEST_FFT 7
#define TEST_NONPERIODIC 8
#define TEST_OVERLAPPING 9
#define TEST_UNIVERSAL 10
#define TEST_APEN 11
#define TEST_RND_EXCURSION 12
#define TEST_RND_EXCURSION_VAR 13
#define TEST_SERIAL 14
#define TEST_LINEARCOMPLEXITY 15
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
G L O B A L D A T A S T R U C T U R E S
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef unsigned char BitSequence;
typedef struct _testParameters {
int n;
int blockFrequencyBlockLength;
int nonOverlappingTemplateBlockLength;
int overlappingTemplateBlockLength;
int serialBlockLength;
int linearComplexitySequenceLength;
int approximateEntropyBlockLength;
int numOfBitStreams;
} TP;
AND here is config.h:
#if defined(__cplusplus)
extern "C" {
#endif
#ifndef _CONFIG_H_
#define _CONFIG_H_
//#define WINDOWS32
//#define PROTOTYPES
//#define LITTLE_ENDIAN
//#define LOWHI
/*
* AUTO DEFINES (DON'T TOUCH!)
*/
#ifndef CSTRTD
typedef char *CSTRTD;
#endif
#ifndef BSTRTD
typedef unsigned char *BSTRTD;
#endif
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#ifndef UINT
typedef unsigned int UINT;
#endif
#ifndef USHORT
typedef unsigned short USHORT;
#endif
#ifndef ULONG
typedef unsigned long ULONG;
#endif
#ifndef DIGIT
typedef USHORT DIGIT; /* 16-bit word */
#endif
#ifndef DBLWORD
typedef ULONG DBLWORD; /* 32-bit word */
#endif
#ifndef WORD64
typedef ULONG WORD64[2]; /* 64-bit word */
#endif
#endif /* _CONFIG_H_ */
#if defined(__cplusplus)
}
#endif
(Phew! So many header files)
Thanks,
GEOFBOT

New Topic/Question
Reply



MultiQuote





|