Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80389 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41652 invoked from network); 12 Jan 2015 00:06:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2015 00:06:00 -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:46231] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E9/31-34383-5EF03B45 for ; Sun, 11 Jan 2015 19:05:58 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 228EFB00099; Sun, 11 Jan 2015 19:05:55 -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 YhTiAPAwhlFY; Sun, 11 Jan 2015 19:05:54 -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 92181B00058; Sun, 11 Jan 2015 19:05:53 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: <54B30DB9.3000003@seld.be> Date: Mon, 12 Jan 2015 00:05:51 +0000 Cc: PHP Internals List Content-Transfer-Encoding: quoted-printable Message-ID: <76B8D342-43E7-42E6-AD82-FCF7C5CA8658@ajf.me> References: <54B30DB9.3000003@seld.be> To: Jordi Boggiano , Yasuo Ohgaki X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] rand(), mt_rand() and limits From: ajf@ajf.me (Andrea Faulds) Hey Yasuo and Jordi, > On 11 Jan 2015, at 23:56, Jordi Boggiano wrote: >=20 > On 11/01/2015 22:12, Andrea Faulds wrote: >> * Make mt_srand() and srand() do nothing and produce a deprecation = notice >=20 > That is the only point with which I disagree. Looking at = http://3v4l.org/FLHBV we see that while indeed across PHP versions the = result has not always been the same, it generally is, and especially = within one given version setting a seed means you get predictable = results. >=20 > This has benefits in some cases like fixtures generation where it = might not be important if the output changes when you upgrade PHP, but = you don't want entirely different fixtures every single time. Obviously = if we could guarantee the algo won't change it would be even better. >=20 > Bottom line is I think it's important to have the ability to set the = seed yourself. >=20 > On 11 Jan 2015, at 23:57, Yasuo Ohgaki wrote: >=20 > However, I object removal of srand. > Game programmers need "the same random sequence" on occasion. > There should be srand to get the same sequence for repeatable = behaviors. >=20 > rand()/srand() may be renamed to sys_rand()/sys_srand() (or whatever=20= > suitable name for them) in case user needs system random sequence=20 > for whatever reasons. I don't insist to keep system's rand/srand, = though. I don=E2=80=99t disagree with having some mechanism for predictable = random number generation from a seed, but I think the global random = number generator is the wrong place to do it. It=E2=80=99s not = guaranteed to be predictable, and everything uses it, so some library = you=E2=80=99re using might advance it without you realising. Much better would be to add a new, OOP API that gives you your own = number generator (no global state) and requires explicitly specifying = the algorithm (cross-version compatibility), with a guarantee that it = won=E2=80=99t break in new PHP versions. Something like this, maybe: $numgen =3D new RandomNumberGenerator(RAND_MERSENNE_TWISTER, = time()); // could auto-seed with time() $randInt1 =3D $numgen->getInt(0, 100); // gets random integer and = advances this generator list($randInt2, $numgen) =3D $numgen->newGetInt(0, 100); // gets = random integer and returns a new, advanced generator $serialised =3D $numgen->serialiseState(); // Or maybe = $numgen->getSeed() ? Does that work? -- Andrea Faulds http://ajf.me/