Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Aug 2014 21:28:48 +0800
From:      Chenguang Li <horus.li@gmail.com>
To:        Peter Pentchev <roam@ringlet.net>
Cc:        freebsd-hackers@freebsd.org, Vitaly Magerya <vmagerya@gmail.com>
Subject:   Re: On changing rand(3) to random(3) in awk(1)
Message-ID:  <5C40F611-22EB-49E4-8925-37922E771C0F@gmail.com>
In-Reply-To: <20140828131526.GA2385@straylight.m.ringlet.net>
References:  <F70B9462-0898-47EF-AF83-47509F21F84E@gmail.com> <53FEFBB8.5040305@gmail.com> <20140828131526.GA2385@straylight.m.ringlet.net>

next in thread | previous in thread | raw e-mail | index | archive | help
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

> Peter Pentchev <roam@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!

Chenguang Li

-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJT/y6XAAoJELG4cS+11lRhQwEQALocD2J3I+xbGYqKGkSQ8Irr
KnuZ21QJ8H+3eb9KLcJ24Tob/vdVi6eukHIDn1SbmDfdyUfm/ufKBObiQ98Qsyu2
/7PB9BcH3N6f9obkFEJWkBEx5Gc3HOAkFhsJFgRvutGVAi1ISTwmqyEcC+18DgqC
NB8j0c5CoFLI03rtJNH+m0Jx5PGPOIHK7t9MrHhKlRmxqbN+pClKDXWLp4/Qfsj9
I6BMLcXcdDMGJ/L81AQUFPbOUXlibjG5m2afV6PSyNwcrPaUT9yyL4lDUunz3IAo
ktGs2eDIiGXM36Iqh4jUk9bCXvJfYf1XuHEUCvhnvVHECDJGv6rJlFU3Eq0M5naN
ZMVc16zuxWg4J7BLwTqI1ocKD1TigvScnpWWjbfr53WEVRXlBlUHcmwHVyZ2knn5
NAVxF/kp9dB8XqJdIVkoFvge+tO9m+HKEtUxALRwjjql/xX0tNEY/F/lcipv4qUk
BKHUPauA5tupolFiI4ucWpOS7XbQBGRQdTEM4CwVrE/wkp6CeTMBagefy0UuGzi1
L/3gq0oMbr9yYjBf8flxO8jz1wbuWIVG00cNUW+ps0ixDsGiD/ea6nfwa8vsx2i3
FnicLPk+ulhlyPqKU70de2Jj7UtJuiVAtQCzEdP3AyJ757V9+y7epMzI7Z5nLHHZ
Q1f8DkLLZdyMDlG0siL4
=bY6X
-----END PGP SIGNATURE-----



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5C40F611-22EB-49E4-8925-37922E771C0F>