linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* random(3) and RAND_MAX
@ 2020-06-05 17:21 John Marshall
  2020-06-06 12:45 ` AW: " Walter Harms
  0 siblings, 1 reply; 8+ messages in thread
From: John Marshall @ 2020-06-05 17:21 UTC (permalink / raw)
  To: mtk.manpages; +Cc: linux-man

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-06-08 20:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 17:21 random(3) and RAND_MAX John Marshall
2020-06-06 12:45 ` AW: " 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)

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).