USE SIZEOF_LONG for high bit grab from random()

This commit is contained in:
Radim Kolar 2024-07-08 13:02:17 +02:00
parent 8b87dfd6da
commit 94c22dd6f8
2 changed files with 18 additions and 2 deletions

View File

@ -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

View File

@ -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: */