From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 28 13:29:09 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C6A19B9F for ; Thu, 28 Aug 2014 13:29:09 +0000 (UTC) Received: from mail-pa0-x234.google.com (mail-pa0-x234.google.com [IPv6:2607:f8b0:400e:c03::234]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 99D3414BD for ; Thu, 28 Aug 2014 13:29:09 +0000 (UTC) Received: by mail-pa0-f52.google.com with SMTP id eu11so2560325pac.11 for ; Thu, 28 Aug 2014 06:29:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=un5gfllXmtWD4NsiAoZnauUNzkV1yVaQqWbiU92ORYM=; b=FEpTe2Htz276RiN6SR7qswahWHd/UN869+tAe2JXps5jq6kBM4PWY9DrnffUYqqqtL msjG/bYyaVl6gX0Q+Z/JTrHTAJkGxk/plQkBeVOnXZlnIENFa9f7UOS5bKRGH9L9m2Tb Qsj4W/dRhUtL7aoN9mJnCFCzwIdD4/WUmuH42g2nOUumFWBob6FCqPzSiT4yIkRmjlYt K8ZaWllI0AXC26Fa+RKYpwiyovr4rrFUpDZNXtKPBKFO9UnJgQA0eqfDo8Juj6FidxKG q1Nnq5a1KNNp3JtkVuiAUnb/SdFhghiocVMV9VEVCyBFYUIwupcmARY0G2gqisWFlEri Npuw== X-Received: by 10.66.121.137 with SMTP id lk9mr6050281pab.86.1409232547098; Thu, 28 Aug 2014 06:29:07 -0700 (PDT) Received: from [127.0.0.1] ([202.55.17.37]) by mx.google.com with ESMTPSA id bq17sm12737255pac.47.2014.08.28.06.29.04 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Aug 2014 06:29:06 -0700 (PDT) Content-Type: text/plain; charset="utf8"; Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: On changing rand(3) to random(3) in awk(1) X-Pgp-Agent: GPGMail (null) From: Chenguang Li In-Reply-To: <20140828131526.GA2385@straylight.m.ringlet.net> Date: Thu, 28 Aug 2014 21:28:48 +0800 Content-Transfer-Encoding: 8bit Message-Id: <5C40F611-22EB-49E4-8925-37922E771C0F@gmail.com> References: <53FEFBB8.5040305@gmail.com> <20140828131526.GA2385@straylight.m.ringlet.net> To: Peter Pentchev X-Mailer: Apple Mail (2.1878.6) Cc: freebsd-hackers@freebsd.org, Vitaly Magerya X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Aug 2014 13:29:10 -0000 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 > Peter Pentchev 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 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-----