diff --git a/ChangeLog b/ChangeLog index 3baee2b..2456d6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -28,6 +28,8 @@ Version NEXT randomcheck: ability to run group tests with different seeds this will more balance results to be less initial seed dependant. classic fsp algo seems to produce best results. + randomcheck: added skip8 method. skips 8 lowest bits from + random() output Version 2.8.1b29 - 24 Aug 2019 added scons command line argument without-fspscan=yes for building diff --git a/tests/randomcheck.c b/tests/randomcheck.c index 4d3f982..920bef6 100644 --- a/tests/randomcheck.c +++ b/tests/randomcheck.c @@ -4,7 +4,8 @@ #include "my-string.h" #include -static int group[4]; +#define GROUPS 5 +static int group[GROUPS]; static int bitcount[16]; static int rounds; static int groups; @@ -45,7 +46,11 @@ static unsigned short simple2 (void) { /* we assume that our return value is 16 bits long and random() returns only positive long integers - */ + */ + /* TODO: this needs to be checked on 64-bit systems because + I am not sure if random returns 31 bits or 63 bits of + randomness there. + */ return ( random() >> (SIZEOF_LONG*8 - 16 - 1) ); } @@ -57,6 +62,13 @@ static unsigned short nr(void) return(ulRandom); } +/* Skip lowest 8 bits */ +static unsigned short skip8(void) +{ + long randomValue = random(); + return randomValue >> 8; +} + static void run_randomtest( unsigned short (*keygen)(void) ) { int i,j; @@ -88,9 +100,9 @@ static void print_bitcount(void) for(i=0;i<16;i++) { float ratio; - float delta; + float delta; ratio=(float)bitcount[i]/rounds; - delta = fabs(ratio-0.5f); + delta = fabs(ratio-0.5f); if(delta > worst) worst=delta; printf("%.2f ", ratio); @@ -107,9 +119,9 @@ static float worst_bitcount(void) for(i=0;i<16;i++) { float ratio; - float delta; + float delta; ratio=(float)bitcount[i]/rounds; - delta = fabs(ratio-0.5f); + delta = fabs(ratio-0.5f); if(delta > worst) worst=delta; } @@ -118,7 +130,7 @@ static float worst_bitcount(void) static void run_one(void) { - float worst[4]; + float worst[GROUPS]; memset(worst, 0, sizeof(worst)); run_randomtest(classic); @@ -133,16 +145,19 @@ static void run_one(void) run_randomtest(nr); worst[3] = worst_bitcount(); + run_randomtest(skip8); + worst[4] = worst_bitcount(); + /* fimd the best algo */ float minValue = worst[0]; int minIndex = 0; - for (int i = 1; i < 4; i++) + for (int i = 1; i < GROUPS; i++) { if (worst[i] < minValue) { minValue = worst[i]; - minIndex = i; + minIndex = i; } } @@ -180,35 +195,36 @@ int main(int argc,const char *argv[]) if ( groups == 1 ) { - seed_rng(); - printf("Generator: classic\n"); - run_randomtest(classic); - print_bitcount(); + seed_rng(); + printf("Generator: classic\n"); + run_randomtest(classic); + print_bitcount(); - printf("Generator: simple (low bits)\n"); - run_randomtest(simple); - print_bitcount(); + printf("Generator: simple (low bits)\n"); + run_randomtest(simple); + print_bitcount(); - printf("Generator: simple2 (high bits)\n"); - run_randomtest(simple2); - print_bitcount(); + printf("Generator: simple2 (high bits)\n"); + run_randomtest(simple2); + print_bitcount(); - printf("Generator: Numerical Recipes\n"); - run_randomtest(nr); - print_bitcount(); + printf("Generator: Numerical Recipes\n"); + run_randomtest(nr); + print_bitcount(); } else { - int i; - for (i=0; i< groups; i++) - { - seed_rng(); - run_one(); - } + int i; + for (i=0; i< groups; i++) + { + seed_rng(); + run_one(); + } - printf("Winning generators\n\n"); - printf("Generator: classic %d\n", group[0]); - printf("Generator: simple (low bits) %d\n", group[1]); - printf("Generator: simple2 (high bits) %d\n", group[2]); - printf("Generator: Numerical Recipes %d\n", group[3]); + printf("Winning generators\n\n"); + printf("Generator: classic %d\n", group[0]); + printf("Generator: simple (low bits) %d\n", group[1]); + printf("Generator: simple2 (high bits) %d\n", group[2]); + printf("Generator: Numerical Recipes %d\n", group[3]); + printf("Generator: skip8 (skip 8 lower bits) %d\n", group[4]); } return result;