On changing rand(3) to random(3) in awk(1)

Peter Pentchev roam at ringlet.net
Thu Aug 28 13:26:07 UTC 2014


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?

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/cc57d0dc/attachment.sig>


More information about the freebsd-hackers mailing list