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

next in thread | previous in thread | raw e-mail | index | archive | help

--RnlQjJ0d97Da+TV1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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_MAX=
 */
> >-               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_MAX=
 */
> >-               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?

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

--RnlQjJ0d97Da+TV1
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJT/ytqAAoJEGUe77AlJ98TAB0P/i+SiOsXSe5RYh19zSzVxvUB
pXA5fMh2VGU69vT0cuy4aLgWzyyo6vX6fMMbuphYXRRa7RJIyf8XjqYnQPzKphP3
Xj3qs20kcFCPRUXr3LAM8kfOcGjGi++ZIDaEJSYpgqMGsmG7uWJ4cbv0LX6+HfxY
3GJTmBirvVENVwMOA+Rbd6WSH3+42+4eOJhXdtlQWbRB9SzhghIbCME8jMkPbXzS
tsXKY3al7x8kYWhV/gQhffPUyFIjRfSe5uU0R9gVsUg5D1k9Z6s2bjX9UJtVAqBE
OGYpHaOMffYFymJB8XE5MXHQzDbbOexsiD3v1JRWD7TLD/KaUgSkMiuhMM8I1Ynq
Qec8Mnmpu40yI0ytTMuQl/Lbt/Ib/nVau5BBuI0HvdFm+jDXvnPtmo4OT5QDlUVt
MOLP+DeN3Q8B9Z2+hV5FOZFPf9/t7w1gLV6c21t8Er9p1lzGP+rakNiT8cZUANVr
kuFY/2E0EoYQYNFnOSgSTh9/twUj8Tyh/cOsPSmLUhp8Ol5rf73eNiorilQcbPmE
/UU6AVrwhfaOXqjzbqP/JZ2u44sqQ3VNJBUnfSCEiKTu34/BC2Tax4S+/PluCK5n
vLH8ICuAs/3MRzeZSZ+/tLCqvEhwstiSzYl4lr2Wvf+000W6uoi8bY6/cno7McBl
OtdvZ/rL83V5qjFzGnvp
=6Ojf
-----END PGP SIGNATURE-----

--RnlQjJ0d97Da+TV1--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140828131526.GA2385>