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: Sat, 14 Aug 2021 12:06:00 +0100	[thread overview]
Message-ID: <86sfzcjnfr.fsf@gmail.com> (raw)
In-Reply-To: <855zzpf86u.fsf@gmail.com>

On Sat 14 Aug 2021, Eli Zaretskii wrote:

>> From: Andy Moreton <andrewjmoreton@gmail.com>
>> Date: Sat, 14 Aug 2021 09:31:06 +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':

    int
    random (void)
    {
      /* rand_as183 () gives us 15 random bits.  */
      return rand_as183 ();
    }

That should result in 'get_random' receiving 15 bits of randomness in
each loop iteration and thus computing a valid result.

[This could obviously be optimised to open code 'rand_as183' in 'random',
 or allow the compiler to inline it by moving the w32.c implementations
 of 'random' and 'srandom' into sysdep.c]

As 'get_random_bignum' (in fns.c) calls 'get_random' in a loop, that
should also remove bugs from that function on this platform.


Perhaps this would be helped by having a test for 'get_random', to check
that every bit of a fixnum is toggled after a reasonable number of
calls. While that does not test the statistical distribution of the
random number sequence, it would ensure that the values returned by
'get_random' are not always positive, or always even, etc.

    AndyM







  reply	other threads:[~2021-08-14 11:06 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
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 [this message]
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=86sfzcjnfr.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).