Date: Thu, 28 Aug 2014 16:38:46 +0300 From: Peter Pentchev <roam@ringlet.net> To: Chenguang Li <horus.li@gmail.com> Cc: freebsd-hackers@freebsd.org, Vitaly Magerya <vmagerya@gmail.com> Subject: Re: On changing rand(3) to random(3) in awk(1) Message-ID: <20140828133846.GB2385@straylight.m.ringlet.net> In-Reply-To: <5C40F611-22EB-49E4-8925-37922E771C0F@gmail.com> References: <F70B9462-0898-47EF-AF83-47509F21F84E@gmail.com> <53FEFBB8.5040305@gmail.com> <20140828131526.GA2385@straylight.m.ringlet.net> <5C40F611-22EB-49E4-8925-37922E771C0F@gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--mojUlQ0s9EVzWg2t Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Aug 28, 2014 at 09:28:48PM +0800, Chenguang Li wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA512 >=20 > > 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 > >>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > >>> --- run.c (revision 270740) > >>> +++ run.c (working copy) > >>> @@ -1522,7 +1522,7 @@ > >>> break; > >>> case FRAND: > >>> /* in principle, rand() returns something in 0..RAND_MA= X */ > >>> - u =3D (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; > >>> + u =3D (Awkfloat) (random() % RAND_MAX) / RAND_MAX; > >>=20 > >> 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). > >>=20 > >> A better patch would be something like this: > >>=20 > >> - /* in principle, rand() returns something in 0..RAND_M= AX */ > >> - u =3D (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; > >> + /* random() returns values in [0, 2147483647] */ > >> + u =3D (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? >=20 > 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 --=20 Peter Pentchev roam@ringlet.net roam@FreeBSD.org p.penchev@storpool.com PGP key: http://people.FreeBSD.org/~roam/roam.key.asc Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13 --mojUlQ0s9EVzWg2t Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCAAGBQJT/zDeAAoJEGUe77AlJ98TWzUP/ApHYpwoiHG+Blo5AuPItI+U 8/5EBQpjcggTvZxwsmOHZfsWTWN1wNIOpfoaTn8XHGgHUraysXq39EJWJgGfLyf6 oZjRlr7vMXRf/km5A9unMQLLHteHfBqwkBBtqpJfdvkMZmwKV9xXar62SBAZJFCh uykfLy8H3ceqs47DI4AjrsAjmqLXKt8w2kSpWSzLHf1PFPS8Ow8ZhOV+j2jnvNsx P3nAzOkspPiAotEwI8S1ss4K0r6eP9Vw1crftH3stYhBfDiK3VdrE/uhghZrJwxt 1Yjy7ZapKdqin5q5osYBoRyRwNGtoty4gHUCPuFgk8F9+lUPX7CFEAQjEZIeAQoN vg3JuuQlDQXpgOELQrRohxBi/6i5MeaztqkmXczzP/+lHWVYkiEbIrtCainuA1IY I8Lg9DJ8H8CibJlMxy8tIElyqEcfS9w7SDnS3GLzeiz9UuYQDtJqGxLvkzMIS8tA 1UHVoveD5ihL+OQQx2UCYfXGwgnKrhe8UxTAGoczStXDqACps0U4zY9c5bUqk2l5 BexRwEryKwiPOXR4tjb9vCp5sBj1oUIgsXzzcpoX5rWvIjN8QLVLLERLiQT7K9mB GqLUsFw5VGx6FC834ohYy84nMRTlMrVymfJL04bLcI5rr03zCWj4BL9mMFpr1B2V hZzE937WTmhzpe8RUOfC =b6nG -----END PGP SIGNATURE----- --mojUlQ0s9EVzWg2t--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140828133846.GB2385>