diff --git a/ChangeLog b/ChangeLog index 5bd8b25..7db8bd8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,7 @@ Version NEXT parsecheck: report if all tests passed / some failed parsecheck: init all fields in testcase results SConstruct: declare CPP constant SIZEOF_LONG + randomcheck: use SIZEOF_LONG for grabing highest bits from long() 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 12e9a1e..cbdaee0 100644 --- a/tests/randomcheck.c +++ b/tests/randomcheck.c @@ -9,6 +9,12 @@ static int rounds; static int result; #define MAX_WORST_ALLOWED 0.1f +/** +Tests how to get 16 bit unsigned short number +from long random(void) function without +losing randomness +*/ + /* FSP classic algo */ static unsigned short classic (void) { @@ -26,10 +32,19 @@ static unsigned short simple (void) return random(); } -/* get high bits from random result - better */ +/* get high bits from random() result. + +In most classic random implementations used in libc +highest bits have better randomness. + +In modern random() generators there should be no difference. +*/ static unsigned short simple2 (void) { - return (random() >> 15); + /* we assume that our return value is 16 bits long + and random() returns only positive long integers + */ + return ( random() >> (SIZEOF_LONG*8 - 16 - 1) ); } /* The following algorithm is recommended by Numerical Recipies: */