AW: random(3) and RAND_MAX

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi John,
i have read the pages and you are right. POSIX says:
rand -> 0...RAND_MAX
random -> 0...2^31-1

that RAND_MAX is 2^31-1 in some cases does not matter. IMHO
it is wrong to mention RAND_MAX in the random page. it can
simply be replaced with (2**31-1)


re,
 wh
________________________________________
Von: linux-man-owner@xxxxxxxxxxxxxxx <linux-man-owner@xxxxxxxxxxxxxxx> im Auftrag von John Marshall <John.W.Marshall@xxxxxxxxxxxxx>
Gesendet: Freitag, 5. Juni 2020 19:21:00
An: mtk.manpages@xxxxxxxxx
Cc: linux-man@xxxxxxxxxxxxxxx
Betreff: random(3) and RAND_MAX

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




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux