On changing rand(3) to random(3) in awk(1)
Peter Pentchev
roam at ringlet.net
Thu Aug 28 13:39:50 UTC 2014
On Thu, Aug 28, 2014 at 09:28:48PM +0800, Chenguang Li wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> > Peter Pentchev <roam at ringlet.net> wrote:
> >> On Thu, Aug 28, 2014 at 12:51:52PM +0300, Vitaly Magerya wrote:
> >>> On 2014-08-28 09:21, Chenguang Li wrote:
> >>> [snip]
> >>> Index: run.c
> >>> ===================================================================
> >>> --- run.c (revision 270740)
> >>> +++ run.c (working copy)
> >>> @@ -1522,7 +1522,7 @@
> >>> break;
> >>> case FRAND:
> >>> /* in principle, rand() returns something in 0..RAND_MAX */
> >>> - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX;
> >>> + u = (Awkfloat) (random() % RAND_MAX) / RAND_MAX;
> >>
> >> You should not use RAND_MAX with random(3), since it returns values
> >> between 0 and 0x7fffffff (inclusive); RAND_MAX only applies to rand(3).
> >>
> >> A better patch would be something like this:
> >>
> >> - /* in principle, rand() returns something in 0..RAND_MAX */
> >> - u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX;
> >> + /* random() returns values in [0, 2147483647] */
> >> + u = (Awkfloat) random() / 2147483648;
> >
> > Hm. Since random() is documented to return "long", wouldn't it be a bit
> > better with #include <limits.h> and LONG_MAX here?
>
> I am using RANDOM_MAX here to maintain the original code structure.
> I've updated my patch, please check the gist. Thanks!
Right, but you've hard-coded RANDOM_MAX to 2^32-1. I don't think this
is correct; I do believe that you should use LONG_MAX for this value.
Of course, #define RANDOM_MAX LONG_MAX could be fine for this purpose
here, but the point is to use LONG_MAX instead of 2^32-1.
G'luck,
Peter
--
Peter Pentchev roam at ringlet.net roam at FreeBSD.org p.penchev at storpool.com
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20140828/af5954c9/attachment.sig>
More information about the freebsd-hackers
mailing list