Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112928 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99231 invoked from network); 19 Jan 2021 16:39:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Jan 2021 16:39:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ACC271804A7 for ; Tue, 19 Jan 2021 08:19:43 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 19 Jan 2021 08:19:43 -0800 (PST) Received: by mail-lf1-f48.google.com with SMTP id 23so29841486lfg.10 for ; Tue, 19 Jan 2021 08:19:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3eON+V1qBaULRxjMRdkka+DnOKozmhJZMFRkRADjkQk=; b=hhOet8lz7ZstPU0zhtuX4yrHYoqlh+iUzpnO+NnRgHyt9O43qFtfiX1d2/NkGx9gxJ 8VtOihK0WXsntT5K567LxGJSBn53wVEX64lZUJ81+4tlaMQ2/BID4u9k+zfJmovxywA3 lze/kIrWT7l2rEqsm0eH0G5AUPYuroBPghGX+z9D2c1ib2EHpoAZtYG9ia5Oyx6OguPG pY+rJQDzxdImM/YQCrevmMeC/qVvtKr6sRJk4JAnamaBqorzqpQJ3GXMWz4jKqjCyBGf QI6lZgmtD1B36zFfZtjJDMWIV5DYtyjElbdFAl5tM+NuhEXppok9oTAcOj1pOqrwdoYO fXYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3eON+V1qBaULRxjMRdkka+DnOKozmhJZMFRkRADjkQk=; b=FgsA01d7MN8frYMVjIK+mmJfiAJSytuW1UM+cu61ZwPzsdMFtrI4zB3fwASJyErbsC wM4aet1bpvBbJnpXYhk41BAl95kkLhKCw4uXL9o4I5hxEwRqW/syvC1Ntcj2pTmAQ4X1 fWEIoKW96Natz835wude+HGkdvb5rrH0rwGqabooImyYf8HFWu4LOcUm5mVcXejDQA/S QnqU+qS2yqtxvkJ+puay0vFt0A1wSxzSUq53y9cXkQ4Nv7VJWXMl7eRf8lFCzU0oyUXC dlP/NdrDHDg+O/Lhe9z0KUISKSlq4M/rHVyYlyjF5IWMmTn5ifOEkV4qtsdrnGs/rH75 BhCw== X-Gm-Message-State: AOAM530RpaFolEgPbIPTWawdF9qRJKA6yq1VbVzkygNKrjoDa7dqOXbg bKSjn19PydHOqb8kcD66EmzLbjUWBu2NzZdKTsY= X-Google-Smtp-Source: ABdhPJw2ZrAocZiLwiJ2wZdkY+3GqapAlbiFpOnxt7TbTDspjFHoFvnUUrwa4HnT/SCtM14oeWWzq9qC5napMVVJwPo= X-Received: by 2002:a19:ed6:: with SMTP id 205mr2192023lfo.159.1611073179907; Tue, 19 Jan 2021 08:19:39 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 19 Jan 2021 17:19:23 +0100 Message-ID: To: Go Kudo Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000987ad205b943340c" Subject: Re: [PHP-DEV] Re: [RFC] Object-scope RNG implementation From: nikita.ppv@gmail.com (Nikita Popov) --000000000000987ad205b943340c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jan 18, 2021 at 5:29 PM Go Kudo wrote: > RFC and implementation updated. > > https://wiki.php.net/rfc/object_scope_prng > https://github.com/php/php-src/pull/6568 > > > MT19937 returns a signed 32bit number as a result of the `next()` metho= d; > the mt_rand() function returns a bit-shifted unsigned 31bit number, so th= e > results are not identical. > > The `rng_range()` function has been renamed to `rng_rand()` and returns a= n > unsigned 31-bit integer like the `mt_rand()` function if only the $rng > argument is received. > > ```php > mt_srand(1234); > mt_rand() =3D=3D=3D rng_rand(new \RNG\MT19937(1234)); // true > ``` > > This allows to completely replace the `mt_rand()` function with the > RNG\MT19937. > > I said I wanted to initiate a vote, but there seems to be too much > discussion missing for that. > I'd like to wait and see for a while. > > Regards, > Go Kudo > > 2021=E5=B9=B41=E6=9C=8817=E6=97=A5(=E6=97=A5) 20:02 Go Kudo : > > > Updated the RFC and fixed the implementation. > > Also made some additions to the RFC about when this feature might be > > useful. > > > > RFC: https://wiki.php.net/rfc/object_scope_prng > > Implementation PR: https://github.com/php/php-src/pull/6568 (All CI > > passed) > > > > The main points are as follows: > > > > - The implementation now meets all requirements. (maybe) > > - Implemented MT19937, which is compatible with PHP standard MT. The > > consistency of these is ensured by tests. > > - The implementation is now what we used to call TypeII. > > - All RNG implementations can now be inherited like regular classes. Us= e > > faster calls only if the class is an internal class. > > - The implementation has been heavily modified and the quality of the > code > > has been improved. > > > > Currently, there are a few concerns: > > > > - MT19937 returns a signed 32bit number as a result of the `next()` > > method; the mt_rand() function returns a bit-shifted unsigned 31bit > number, > > so the results are not identical. > > - You can get the equivalent result with `$rng->next() << 1 & > > PHP_INT_MAX`, but maybe you should have a `nextUnsigned31()` method. > > - Currently, the test uses `$rng->next() << 1 & PHP_INT_MAX`. > > - The implementation of MT19937 is redundant. This can be merged. > > > > Anyway, I think the code is finally ready for review. > > Once that is done, we will start voting on RFCs. > > > > Regards, > > Go Kudo > Overall I like the proposal. Some notes: * I know you only just changed it, but I don't think rng_rand() should support calling without arguments. This is a backwards-compatibility leftover in mt_rand() and we should not carry it over into a new API. * You should probably mention that the classes can be serialized in the RFC. Maybe you want to write out all the methods so it's also visible how the constructors for the classes look like. * Naming: RNG\RNGInterface seems a bit redundant. Of course, we can't make it RNG\Interface. RNG\Generator would be an option. I'm not sure about this. Maybe RNG\RNGInterface is fine. * 64-bit: I looked over your implementation, and I think your approach to handling 64-bits is correct. You only call next64() if the requested range is larger than 32-bit, and that can only happen on 64-bit systems, thus guaranteeing consistency of results (where they can be consistent at all). What I'm unsure about is the way this gets exposed to userland via next() and next64() methods. I think fetching of 64-bit values is mainly interesting as an internal optimization, and not so much important to be part of the RNGInterface API. The user is intended to get numbers via rng_rand(), not by directly calling next(). A possibility here would be to drop the RNG64Interface and next64() method, have next() always return 32-bit, but retain the internal next64 API. For this to make sense, we'd need two subsequent internal next() calls to return the same data as a single next64() call (i.e. store the result and first return one half then the other). Would this make sense to you? Regards, Nikita --000000000000987ad205b943340c--