unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Andy Moreton <andrewjmoreton@gmail.com>
To: 32605@debbugs.gnu.org
Subject: bug#32605: [w64] (random) never returns negative
Date: Fri, 13 Aug 2021 22:12:29 +0100	[thread overview]
Message-ID: <861r6xoxqa.fsf@gmail.com> (raw)
In-Reply-To: <855zzpf86u.fsf@gmail.com>

On Fri 13 Aug 2021, Eli Zaretskii wrote:

>> From: Andy Moreton <andrewjmoreton@gmail.com>
>> Date: Thu, 12 Aug 2021 21:34:09 +0100
>> 
>> >     int val = ((rand_as183 () << 15) | rand_as183 ());
>> >   #ifdef __x86_64__
>> >     return 2 * val - 0x3FFFFFFF;
>> >   #else
>> >     return val;
>> >   #endif
>> >
>> > Andy, can you test this, please?
>> 
>> That does not produce any negative random numbers within a reasonable
>> number of attempts (a few dozen calls).
>
> Thanks for testing.

You elided the detail of my previous message:

  Surely real the problem is that RAND_BITS is 31, but the random() in
  w32.c does not provide 31 random bits (and thus fails to meet the API
  contract).

  In 32bit builds this problem is hidden because 30 bits are sufficient
  for a fixnum, so the value of bit30 in the result is ignored.

  On 64bit builds, 62 bits are needed for a fixnum, and trying to assemble
  a random number from multiple components does not work if RAND_BITS says
  31 bits are usable, but the highest bit in that value is always zero.

Please answer that. This function appears to not work properly at all.

>> Instead, calling rand_as183 again (as below) does produce positive and
>> negative random numbers on 32bit and 64bit builds with a similar number
>> of attempts:
>> 
>> return ((rand_as183 () << 30) | (rand_as183 () << 15) | rand_as183 ());
>> 
>> While this may be less efficient, it at least meets the contract of
>> providing 31 random bits.
>
> What about the variant below, does it produce better results?
>
>     int val = ((rand_as183 () << 15) | rand_as183 ());
>   #ifdef __x86_64__
>     return 2 * val - 0x7FFFFFFF;
>   #else
>     return val;
>   #endif

Why is this any better ? On 32bit builds it does not return 31 random
bits (only a 30bit value) and on 64bit builds the lowest bit is not
random.

I'm not sure I see the point of this bit manipulation as it does not
spread randomness throughout the bits in the returned value.

    AndyM






  reply	other threads:[~2021-08-13 21:12 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-01 17:18 bug#32605: 26.1; (random) never returns negative Francis Wright
2018-09-01 17:34 ` Stephen Berman
2018-09-04 22:27   ` Noam Postavsky
2018-09-05 13:20     ` Andy Moreton
2021-08-12 13:17       ` bug#32605: [w64] " Lars Ingebrigtsen
2021-08-12 13:42         ` Eli Zaretskii
2021-08-12 20:34           ` Andy Moreton
2021-08-13  6:29             ` Eli Zaretskii
2021-08-13 21:12               ` Andy Moreton [this message]
2021-08-14  5:54                 ` Eli Zaretskii
2021-08-14  8:31                   ` Andy Moreton
2021-08-14  8:57                     ` Eli Zaretskii
2021-08-14 11:06                       ` Andy Moreton
2021-08-14 11:33                         ` Eli Zaretskii
2021-08-14 12:10                           ` Andy Moreton
2021-08-14 12:36                             ` Eli Zaretskii
2021-08-14 13:40                               ` Andy Moreton
2021-08-14 14:43                                 ` Eli Zaretskii
2021-08-14 18:47                                   ` Andy Moreton
2021-08-15  6:07                                     ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=861r6xoxqa.fsf@gmail.com \
    --to=andrewjmoreton@gmail.com \
    --cc=32605@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).