Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114782 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29647 invoked from network); 8 Jun 2021 12:18:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Jun 2021 12:18:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 01F811804D8 for ; Tue, 8 Jun 2021 05:33:10 -0700 (PDT) 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-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (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, 8 Jun 2021 05:33:09 -0700 (PDT) Received: by mail-ej1-f43.google.com with SMTP id he7so12915006ejc.13 for ; Tue, 08 Jun 2021 05:33:09 -0700 (PDT) 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; bh=JgtOkDXYpfddOh3NeUb+cfQQtzedfast/N55mzeS39w=; b=GTFHV1d5MU9YUWOASQW/VJuZIwJhGLQYDFbPujNYd/j3uwlzhEnBOHiEPYeixbtaLr w7UEcM5HngDWN5gutUQHZClgb5ZibhonEact7+ZFQKPZU0BoGLc2zlAR7AbrU7N0T0yl syjyhNrL0SYbR1Tj3vzMW0Hf6YM5Zx6C5MYTuE+H+hcVBrEnkltrCgSFAR7BZpYkpsWo Ti+B9bbWcjH0cNYrA2sOd6T0HjfLYk77QuMEoOvlWDdqloOOvQS3uNq6P2Uai1/5jIlS 3mrbJUnWSPpQ1NCGwLgrSrKAecgeMFV9qKA5AUSfH2u8Pld/fGmTgxUJ5EDfIZ9EQBHZ NHWQ== 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; bh=JgtOkDXYpfddOh3NeUb+cfQQtzedfast/N55mzeS39w=; b=ed6OSSI8ybrCLiFEik5xBIT7upoCV2hnCxmxWxMembnzr8Esdp6TLDSAFeMSk139H6 GGfN3oIyJii472iJ+/jBZfOYITJ9UwXjzW5QiuqDAmAmOeH3KcGYcWZtvjcKsFPDUQf4 mYLBst5M/7X11LkJNH7JtPqaAYuvMKo2dgfqQTctjdZbS1CgjUaV8xOqTCGSrIlbEiP/ t3jLMgwxA2WW3KzfuNypcrfoCh8gWfYWo5Gh88anacKusdBAFHL8g+4S73LN+YvCOXwr LBk6dFpZHx3smZw/mxc/ULMyn94OFcLMmBIV8pkhKwnacSPC192CxfvQJ601/ItdTswD lycA== X-Gm-Message-State: AOAM531arXIVKFNLfaCHA5sl33KsaFCmK7QAvqGPXHgs88SJ7qW97hDC htuW+8qKj+Tz5pSw0LshOE1QFM6eAkVgBFDW+vlETpwRUvV2xA== X-Google-Smtp-Source: ABdhPJxtm1+OqUC1h78rvj+3lXYnGVKzznys1K+8vq/EZjslCRleV9LiZtvROZ+Xp1UiF+Z+xYkJ4F0BUcf7i7rTlMo= X-Received: by 2002:a17:906:dfd1:: with SMTP id jt17mr23160703ejc.486.1623155584998; Tue, 08 Jun 2021 05:33:04 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 8 Jun 2021 21:32:54 +0900 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000000f226505c4405c9b" Subject: Re: [RFC] Under Discussion: Add Random class and RandomNumberGenerator interface From: zeriyoshi@gmail.com (Go Kudo) --0000000000000f226505c4405c9b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello iinternals. There doesn't seem to be much mention of it. But, that may be because it has been discussed well in advance. Thank you for participating in the discussion. Now, if there is no particular discussion on this, I will try to start the voting phase next week. Of course, I will contact you separately. However, I was looking back at the implementation and found only one point of concern. With the current implementation, the results of the following example will match. ```php $one =3D new Random(); $one->nextInt(); $two =3D clone $one; var_dump($one->nextInt() =3D=3D=3D $two->nextInt()); // true ``` But, this is not true for user implementations. ```php class UserRNG implements RandomNumberGenerator { protected int $current =3D 0; public function generate(): int { return ++$this->current; } } $rng =3D new UserRNG(); $one =3D new Random($rng); $one->nextInt(); $two =3D clone $one; var_dump($one->nextInt() =3D=3D=3D $two->nextInt()); // false ``` This is because `$rng` is kept as a normal property and is managed by the standard PHP mechanism. In other words, it is passed by reference. This is not consistent with the built-in RNG behavior. However, I don't see a problem with this behavior. I feel that the standard PHP behavior is preferable to changing the userland behavior in a specific way. I would like to solicit opinions. Regards, Go Kudo 2021=E5=B9=B46=E6=9C=881=E6=97=A5(=E7=81=AB) 23:28 Go Kudo : > Hello internals. > Thanks for continuing to participate in the discussion. > > I've sorted out the proposal, and finished writing and implementing the > RFC. > (Funny, I know.) I think it's time to start a discussion. > > https://wiki.php.net/rfc/rng_extension > https://github.com/php/php-src/pull/7079 > > The main changes since last time are as follows: > > - The ugly RANDOM_USER has been removed. > - RandomNumberGenerator interface has been added for user-defined RNGs. > - Random class is now final. > - Random class now accepts a RandomNumberGenerator interface other than > string as the first argument to the constructor. > - INI directive has been removed. In 32-bit environments, the result is > always truncated. > > What I'm struggling with now is the behavior when calling nextInt() in a > 32-bit environment using a 64-bit RNG. It currently returns a truncated > result, which means that the code loses compatibility with the result of > running on a 64-bit machine. > I was also considering throwing an exception, but which would be > preferable? > > I would like to answer a few unanswered questions. > > > What is bias? > > I' ve confirmed that the bias issue in mt_rand has already been fixed and > is no longer a problem. > > This implementation copies most of it from mt_rand. Therefore, this > problem does not exist. > > Therefore, an equivalent method to mt_getrandmax() is no longer provided. > > > uint64 & right-shift > > This is a specification of the RNG implementation. PHP does not have > unsigned integers, but RNG handles unsigned integers (to be precise, even > the sign bit is random). > > As mentioned above, PHP does not have unsigned integers, which means that > using the results generated by RNGs may cause compatibility problems in t= he > future. To avoid this, a 1-bit right shift is always required (similar to > mt_rand()). > > > > --0000000000000f226505c4405c9b--