linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Marshall <John.W.Marshall@glasgow.ac.uk>
To: "mtk.manpages@gmail.com" <mtk.manpages@gmail.com>
Cc: "linux-man@vger.kernel.org" <linux-man@vger.kernel.org>
Subject: random(3) and RAND_MAX
Date: Fri, 5 Jun 2020 17:21:00 +0000	[thread overview]
Message-ID: <02185B3C-F6E5-4674-BB34-E549E04C5B37@glasgow.ac.uk> (raw)

Observed in CentOS 8's man-pages-4.15-6.el8.x86_64 and also on man-pages Git master:

Man-pages's rand.3 says rand() returns values in the range [0, RAND_MAX] and is very clear that this is inclusive. This is the same as the POSIX description of rand(3).

Man-pages's random.3 says random() returns values "in the range from 0 to RAND_MAX". However POSIX describes random() as returning values "in the range from 0 to 2^31-1".

In practice glibc and musl both fix RAND_MAX as a constant 2^31-1 so on these platforms it is the same thing. Similarly on macOS. It appears that FreeBSD used to have a slightly lower value of RAND_MAX but several months ago raised it to 2^31-1 similarly. OTOH it appears that Windows, Cygwin, etc still use a much smaller value for RAND_MAX (32767) but the full POSIX range for random(3).

So random.3 describing the range as 0..RAND_MAX is correct on Linux (unless you're using a very unusual libc) but misleading when used as a reference for writing code portable to other platforms. It would be good to change random.3 to refer to the hardcoded constant (2^31-1) instead of RAND_MAX (and perhaps add a note that on Linux this is the same as RAND_MAX), or at least to add a note saying that RAND_MAX may be an unrelated value on other platforms.

Thanks,

    John

             reply	other threads:[~2020-06-05 17:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-05 17:21 John Marshall [this message]
2020-06-06 12:45 ` AW: random(3) and RAND_MAX Walter Harms
2020-06-08 11:04   ` [patch] random.3: wfix: RAND_MAX is for rand(3) John Marshall
2020-06-08 11:10     ` AW: " Walter Harms
2020-06-08 11:20       ` John Marshall
2020-06-08 11:21       ` AW: " Jakub Wilk
2020-06-08 12:50         ` AW: " Walter Harms
2020-06-08 20:27     ` Michael Kerrisk (man-pages)

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=02185B3C-F6E5-4674-BB34-E549E04C5B37@glasgow.ac.uk \
    --to=john.w.marshall@glasgow.ac.uk \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /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 NNTP newsgroup(s).