public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "agriff at tin dot it" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/88935] std::random_shuffle does not work if the sequence is longer than RAND_MAX elements
Date: Wed, 19 Jun 2024 06:38:44 +0000	[thread overview]
Message-ID: <bug-88935-4-qUW3PXSUQg@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-88935-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88935

Andrea Griffini <agriff at tin dot it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |agriff at tin dot it

--- Comment #12 from Andrea Griffini <agriff at tin dot it> ---
Even assuming rand() were generating hardware random numbers (not allowed by
the standard because of srand, obviously), gcc would still be broken and
performing a terrible random_shuffle with ranges larger than 64k elements
(indeed non-uniformity becomes evident even at much smaller ranges).

Mingw's rand() numbers are decent enough (and the period is not just 2**16, for
example) but they're only 16 bit. This is allowed.

The gcc bug is that it uses rand() (that can be just 16) bit to pick a random
number between 0 and the size of the range even when the range is much bigger
than 65536. Using rand() to feed data into a xorshift64 or something similar
only for large ranges would fix, and would also keep shuffle of small ranges
backward compatible (shuffling elements in small ranges exactly as current
implementation does if starting from the same random seed).

I've somewhere an half backed patch for doing that but quite frankly I don't
know if I should polish it and submit here as, in all honesty, seems that in
this case the maintainers simply don't want to have this bug fixed, for reasons
that I really cannot understand.

  parent reply	other threads:[~2024-06-19  6:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-88935-4@http.gcc.gnu.org/bugzilla/>
2024-01-16 15:59 ` xry111 at gcc dot gnu.org
2024-01-16 16:04 ` xry111 at gcc dot gnu.org
2024-01-16 17:31 ` redi at gcc dot gnu.org
2024-01-16 17:31 ` redi at gcc dot gnu.org
2024-01-16 17:37 ` xry111 at gcc dot gnu.org
2024-01-16 21:25 ` redi at gcc dot gnu.org
2024-06-19  6:38 ` agriff at tin dot it [this message]
2024-06-19  8:13 ` redi at gcc dot gnu.org

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

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

  git send-email \
    --in-reply-to=bug-88935-4-qUW3PXSUQg@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.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.
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).