added skip8 method

This commit is contained in:
Radim Kolar 2024-07-09 17:03:39 +02:00
parent b79d034950
commit 21704994bf
2 changed files with 51 additions and 33 deletions

View File

@ -28,6 +28,8 @@ Version NEXT
randomcheck: ability to run group tests with different seeds randomcheck: ability to run group tests with different seeds
this will more balance results to be less initial seed this will more balance results to be less initial seed
dependant. classic fsp algo seems to produce best results. 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 Version 2.8.1b29 - 24 Aug 2019
added scons command line argument without-fspscan=yes for building added scons command line argument without-fspscan=yes for building

View File

@ -4,7 +4,8 @@
#include "my-string.h" #include "my-string.h"
#include <math.h> #include <math.h>
static int group[4]; #define GROUPS 5
static int group[GROUPS];
static int bitcount[16]; static int bitcount[16];
static int rounds; static int rounds;
static int groups; static int groups;
@ -45,7 +46,11 @@ static unsigned short simple2 (void)
{ {
/* we assume that our return value is 16 bits long /* we assume that our return value is 16 bits long
and random() returns only positive long integers 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) ); return ( random() >> (SIZEOF_LONG*8 - 16 - 1) );
} }
@ -57,6 +62,13 @@ static unsigned short nr(void)
return(ulRandom); 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) ) static void run_randomtest( unsigned short (*keygen)(void) )
{ {
int i,j; int i,j;
@ -88,9 +100,9 @@ static void print_bitcount(void)
for(i=0;i<16;i++) for(i=0;i<16;i++)
{ {
float ratio; float ratio;
float delta; float delta;
ratio=(float)bitcount[i]/rounds; ratio=(float)bitcount[i]/rounds;
delta = fabs(ratio-0.5f); delta = fabs(ratio-0.5f);
if(delta > worst) if(delta > worst)
worst=delta; worst=delta;
printf("%.2f ", ratio); printf("%.2f ", ratio);
@ -107,9 +119,9 @@ static float worst_bitcount(void)
for(i=0;i<16;i++) for(i=0;i<16;i++)
{ {
float ratio; float ratio;
float delta; float delta;
ratio=(float)bitcount[i]/rounds; ratio=(float)bitcount[i]/rounds;
delta = fabs(ratio-0.5f); delta = fabs(ratio-0.5f);
if(delta > worst) if(delta > worst)
worst=delta; worst=delta;
} }
@ -118,7 +130,7 @@ static float worst_bitcount(void)
static void run_one(void) static void run_one(void)
{ {
float worst[4]; float worst[GROUPS];
memset(worst, 0, sizeof(worst)); memset(worst, 0, sizeof(worst));
run_randomtest(classic); run_randomtest(classic);
@ -133,16 +145,19 @@ static void run_one(void)
run_randomtest(nr); run_randomtest(nr);
worst[3] = worst_bitcount(); worst[3] = worst_bitcount();
run_randomtest(skip8);
worst[4] = worst_bitcount();
/* fimd the best algo */ /* fimd the best algo */
float minValue = worst[0]; float minValue = worst[0];
int minIndex = 0; int minIndex = 0;
for (int i = 1; i < 4; i++) for (int i = 1; i < GROUPS; i++)
{ {
if (worst[i] < minValue) if (worst[i] < minValue)
{ {
minValue = worst[i]; minValue = worst[i];
minIndex = i; minIndex = i;
} }
} }
@ -180,35 +195,36 @@ int main(int argc,const char *argv[])
if ( groups == 1 ) if ( groups == 1 )
{ {
seed_rng(); seed_rng();
printf("Generator: classic\n"); printf("Generator: classic\n");
run_randomtest(classic); run_randomtest(classic);
print_bitcount(); print_bitcount();
printf("Generator: simple (low bits)\n"); printf("Generator: simple (low bits)\n");
run_randomtest(simple); run_randomtest(simple);
print_bitcount(); print_bitcount();
printf("Generator: simple2 (high bits)\n"); printf("Generator: simple2 (high bits)\n");
run_randomtest(simple2); run_randomtest(simple2);
print_bitcount(); print_bitcount();
printf("Generator: Numerical Recipes\n"); printf("Generator: Numerical Recipes\n");
run_randomtest(nr); run_randomtest(nr);
print_bitcount(); print_bitcount();
} else { } else {
int i; int i;
for (i=0; i< groups; i++) for (i=0; i< groups; i++)
{ {
seed_rng(); seed_rng();
run_one(); run_one();
} }
printf("Winning generators\n\n"); printf("Winning generators\n\n");
printf("Generator: classic %d\n", group[0]); printf("Generator: classic %d\n", group[0]);
printf("Generator: simple (low bits) %d\n", group[1]); printf("Generator: simple (low bits) %d\n", group[1]);
printf("Generator: simple2 (high bits) %d\n", group[2]); printf("Generator: simple2 (high bits) %d\n", group[2]);
printf("Generator: Numerical Recipes %d\n", group[3]); printf("Generator: Numerical Recipes %d\n", group[3]);
printf("Generator: skip8 (skip 8 lower bits) %d\n", group[4]);
} }
return result; return result;