Subject: Re: bug#32605: [w64] (random) never returns negative

From: Andy Moreton <[email-masked]>
Date: Sat, 14 Aug 2021 13:10:08 +0100

On Sat 14 Aug 2021, Eli Zaretskii wrote:

>> From: Andy Moreton <[email-masked]>
>> Date: Sat, 14 Aug 2021 12:06:00 +0100
>> 
>> >> I'm not an expert on random numbers either, and your efforts are not an
>> >> annoyance, but I am puzzled why you so strongly prize performance over
>> >> correctness in this instance.
>> >
>> > Because I have no idea how important the "correctness" part is, or
>> > why.  OTOH, this stuff, when used, tends to be in the inner loops, so
>> > performance matters.
>> 
>> I doubt anyone expects cryptographic quality randomness or any given
>> statistical distribution from such a general purpose routine, but they
>> have a reasonable expectation that the results from 'get_random' do not
>> have stuck bits that are always non-random.
>> 
>> In which case perhaps the solution is to change the RAND_BITS logic
>> in sysdep.c on Windows to override the RAND_BITS definition:
>> 
>> +   #ifdef WINDOWSNT
>> +   /* Use w32.c replacement for random().  */
>> +   # define RAND_BITS 15
>> +   #endif
>> 
>>     #ifndef RAND_BITS
>>     # ifdef HAVE_RANDOM
>>     #  define RAND_BITS 31
>>     # else /* !HAVE_RANDOM */
>>     ...
>>     #endif
>> 
>> ..and then in w32.c make 'random' return the 15bit value from
>> 'rand_as183':
>
> Why not keep the 30 bits we produce today on 32-bit builds?

For 32bit builds (FIXNUM_BITS is 30), either:

 a) define RAND_BITS to 30, 'random' calls 'rand_as183' twice.
    'get_random' needs 1 call to 'random' (total 2 calls of 'rand_as183').

 b) define RAND_BITS to 15, 'random' calls 'rand_as183' once.
    'get_random' needs 2 calls to 'random' (total 2 calls of 'rand_as183').

For 64bit builds (FIXNUM_BITS is 62), either:

 a) define RAND_BITS to 30, 'random' calls 'rand_as183' twice.
    'get_random' needs 3 calls to 'random' (total 6 calls of 'rand_as183').

 b) define RAND_BITS to 15, 'random' calls 'rand_as183' once.
    'get_random' needs 4 calls to 'random' (total 4 calls of 'rand_as183').

On 32bit builds both options are roughly equivalent.
On 64bit builds option (b) is better as option (a) does unnecessary work.

    AndyM