Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80395 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51511 invoked from network); 12 Jan 2015 00:55:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2015 00:55:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.216.42 mail-qa0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:51288] helo=mail-qa0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 74/43-34383-69B13B45 for ; Sun, 11 Jan 2015 19:55:51 -0500 Received: by mail-qa0-f42.google.com with SMTP id n8so13636901qaq.1 for ; Sun, 11 Jan 2015 16:55:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=iFzDYMKPFSa8PmSi1lQyV8FcrZBAz5wc+VSK4o2Ylk8=; b=zdHWsbl7+48ecKFdnJARexu/o+moMh1JVXSsSsb2uLAmjycJD07Q893NS7GbxyzAuX Q5ovzxLMxfczNmTcjh6C8VvD2u+1VidT9JuEtOhagMlQ0MvNt2KwojqhhtFrAVMFzCcW ofgUTZbI5mjaIyAdtRVsLXKpsGfV4mD0cEfrFVW4Jcuo1v6PIT6NqyBef0IftCeDjkiU ZivPmJm5bLrKYPAxqVITJXzBevuB73yw3ivwxdMcQ5bY0kwY1kG5svLVTNC7Ifh1GRWK jIT7t6RMugo9Si35OZknxALMiQR9Y1d3OMQOYYC5hggE3eQOduNs0WGAMSI4T08ZmKnQ tq4w== X-Received: by 10.229.48.132 with SMTP id r4mr45162751qcf.5.1421024148532; Sun, 11 Jan 2015 16:55:48 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.93.70 with HTTP; Sun, 11 Jan 2015 16:55:08 -0800 (PST) In-Reply-To: <76B8D342-43E7-42E6-AD82-FCF7C5CA8658@ajf.me> References: <54B30DB9.3000003@seld.be> <76B8D342-43E7-42E6-AD82-FCF7C5CA8658@ajf.me> Date: Mon, 12 Jan 2015 09:55:08 +0900 X-Google-Sender-Auth: jseOMl_H4fxCfe42ju1HVPlorbA Message-ID: To: Andrea Faulds Cc: Jordi Boggiano , PHP Internals List Content-Type: multipart/alternative; boundary=001a1134050c974378050c69f790 Subject: Re: [PHP-DEV] rand(), mt_rand() and limits From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1134050c974378050c69f790 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Andrea, On Mon, Jan 12, 2015 at 9:05 AM, Andrea Faulds wrote: > > On 11 Jan 2015, at 23:56, Jordi Boggiano wrote: > > > > On 11/01/2015 22:12, Andrea Faulds wrote: > >> * Make mt_srand() and srand() do nothing and produce a deprecation > notice > > > > 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 with= in > one given version setting a seed means you get predictable results. > > > > 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. > > > > Bottom line is I think it's important to have the ability to set the > seed yourself. > > > > > On 11 Jan 2015, at 23:57, Yasuo Ohgaki wrote: > > > > 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= . > > > > rand()/srand() may be renamed to sys_rand()/sys_srand() (or whatever > > suitable name for them) in case user needs system random sequence > > for whatever reasons. I don't insist to keep system's rand/srand, thoug= h. > > I don=E2=80=99t disagree with having some mechanism for predictable rando= m 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 rand= om > integer and returns a new, advanced generator > $serialised =3D $numgen->serialiseState(); // Or maybe > $numgen->getSeed() ? > > Does that work? It works, but I prefer to have procedural API also (and OO API if it is needed.) I like multi paradigm programming language. Pseudo random number generator is pseudo. We are better off with real random generator when cryptographic random number is needed. So renaming mt_rand() -> rand() / rand() -> sys_rand() and keeping mt_rand() alias would be enough. IMO. BTW, difference between 32bit and 64bit platforms would not be a problem as long as it is documented. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a1134050c974378050c69f790--