Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80402 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62227 invoked from network); 12 Jan 2015 01:25:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2015 01:25:36 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.200 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.200 imap1-2.ox.privateemail.com Received: from [192.64.116.200] ([192.64.116.200:56854] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 80/A5-34383-F8223B45 for ; Sun, 11 Jan 2015 20:25:35 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id DB2AEB00096; Sun, 11 Jan 2015 20:25:32 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id T6yoS2R5ENP0; Sun, 11 Jan 2015 20:25:32 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 30FF6B00098; Sun, 11 Jan 2015 20:25:32 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: <54B31F65.90203@gmail.com> Date: Mon, 12 Jan 2015 01:25:30 +0000 Cc: PHP Internals List Content-Transfer-Encoding: quoted-printable Message-ID: References: <54B31F65.90203@gmail.com> To: Stanislav Malyshev X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] rand(), mt_rand() and limits From: ajf@ajf.me (Andrea Faulds) Hey, > On 12 Jan 2015, at 01:12, Stanislav Malyshev = wrote: >=20 >> * Get rid of rand(), srand() and getrandmax() * Rename mt_rand(), >> mt_srand() and mt_getrandmax() to rand(), srand(), and getrandmax() >> but add mt_* aliases for backwards-compatibility=20 >=20 > This means rand() and mt_rand() would do the same. That however = assumes > that OS's libc random-number functions are and will always be = inferior. > Is that the case that we believe? Quite possibly not, but we know that some OSes do have inferior rand() = implementations and using our own ensures cross-platform uniformity. = Using our own implementation shields users from sucky stdlibs. > In any case, I would rather disclaim any specifics about = implementation > of rand() other than saying it is using best algorithm we have. If we > decide MT one is the best we can support, so be it. Yeah, I agree there. >> * Make mt_srand() >> and srand() do nothing and produce a deprecation notice=20 >=20 > I think this is not a good idea. While we can not guarantee the PRNG = we > use always provides the same values for the same srand on every system > for every version, it is the case that it provides them in the same > environment, thus enabling the possibility of testing random-driven > algorithms. Disabling it precludes any such testing, except with > user-level workarounds, which will inevitably be more brittle and > error-prone. This all is for no observable gain. Such algorithms really shouldn=E2=80=99t be using rand() or mt_rand(), = they aren=E2=80=99t fit for purpose for reasons I=E2=80=99ve previously = elaborated. Code which needs to do what you=E2=80=99re describing should use an API = made specifically for that purpose. There are userland packages for = this. We could also add a standard library class like I suggested = earlier. I don=E2=80=99t really see why a userland DRBG would = necessarily be =E2=80=9Cbrittle and error-prone=E2=80=9D. Not unless = they=E2=80=99re using some completely unmaintained library. > * Use a >> 64-bit random number generation algorithm on 64-bit platforms (or >> invoke the 32-bit generator twice) >=20 > This sounds like a good idea, though it may have BC implications. Is > there a use case we know of where it matters? I can=E2=80=99t immediately think of one, but I can=E2=80=99t imagine = there wouldn=E2=80=99t be a need for values larger than 2^32. By the way, I=E2=80=99m not sure what I=E2=80=99ll do for rand() for the = bigint RFC/patch. I might just use the bigint library=E2=80=99s random = function when the range is wider than that a native integer has, but I = can=E2=80=99t do that if we allow seeding (because it breaks predictable = sequence generation). Alternatively, I could just not make it work with = bigints, since producing incredibly large numbers can of course be done = manually with bitwise shifts and multiple mt_rand() invocations. -- Andrea Faulds http://ajf.me/