GNU bug report logs -
#32605
[w64] (random) never returns negative
Previous Next
Reported by: f.j.wright <at> live.co.uk
Date: Sat, 1 Sep 2018 17:21:01 UTC
Severity: normal
Tags: confirmed
Found in version 26.1
Done: Eli Zaretskii <eliz <at> gnu.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
> From: Andy Moreton <andrewjmoreton <at> 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.
> 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
This bug report was last modified 4 years ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.