Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114800 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78882 invoked from network); 9 Jun 2021 15:56:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2021 15:56:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BED7C180502 for ; Wed, 9 Jun 2021 09:11:16 -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-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 ; Wed, 9 Jun 2021 09:11:16 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id bp38so3310299lfb.0 for ; Wed, 09 Jun 2021 09:11:16 -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 :cc; bh=870EDpCWFczXzaRpW2VeEs/Imp6+Wl2WDuljH8kffXY=; b=tdUMrswO06K0RA9nFJH+4VWn1OKIrRt9ep6GpufrIXcT6OUJ4dhM+k/91TIDmJKxuN Tl03PFiTScYeMbPrc2p0SXIiyXOcYeb7vlllGTFxxs+3qOAHREilufwEeeYy/hN9vkVm MDJZDMtGyJtt3UrXJtDOYEdsc8VuK8vkPKefjMq4lVR9Ub/ybW8PHr6D2Vqzw8bjJ8mS 948zwB2flQjE54YxnwiaLTyO7ZYsz0JSsbi9UGduxHerBQix+2wpZG6U/UJ3UroY+4fx ADK1kyOHu/wYYjy8GPq/fviGH7FHZCgcuo+QsaOTU8kgvVnJ7/dALRkyHNdKtIgrq63f Vupg== 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=870EDpCWFczXzaRpW2VeEs/Imp6+Wl2WDuljH8kffXY=; b=hqSQtUe0LpSl6kgzpiIeKKV90BVfupsBTn9ZJFmp2lNpUySDIzmt/4qIWHfUVPRMwJ nh0CtnZiwSzO2sd3oIUD0tC1l15iNJBvpGjUXM6cbc956l32Mydv4XPYH2nsRJtyIjbc b5y3EVamfudJu8uoBOkwkm9eB1aJgKVTh6ETtwpuxwTaWnVYzlDJobqUPxEXCGSSxeFw cfL+mwwBapEqKHzKoQEwupaGqJiiS/KTuU6ifgB4yYq81rGLuuxhxwwaveinRdkMrsnC cLGtHRLktq18TEL90cQewsQ3ZjT2wVFtFasC/ZKhOqF+1LEW4IXs1lDQrGVg/cv/pTDj uflA== X-Gm-Message-State: AOAM530izIoWNiVQo4nQMvcu56dUXif4DSPvEt2ZcdfUJnozWBiBth3E a6LEhz5ziZhKgPC5H7yh+RL7k8lolkTHob8iMw== X-Google-Smtp-Source: ABdhPJz3uh0BcGNJ8codEqs9Hpdcvy+zHEfIam2xCM3Aimr64Cmnf3nqBVQPnIfqZl8umaVMm5oo7wqEs2m4HsVcs20= X-Received: by 2002:a05:6512:992:: with SMTP id w18mr177861lft.650.1623255073700; Wed, 09 Jun 2021 09:11:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 9 Jun 2021 18:11:02 +0200 Message-ID: To: Go Kudo Cc: PHP internals , Nikita Popov Content-Type: multipart/alternative; boundary="0000000000000c3d6a05c4578689" Subject: Re: [PHP-DEV] Re: [RFC] Under Discussion: Add Random class and RandomNumberGenerator interface From: guilliam.xavier@gmail.com (Guilliam Xavier) --0000000000000c3d6a05c4578689 Content-Type: text/plain; charset="UTF-8" On Wed, Jun 9, 2021 at 3:16 PM Go Kudo wrote: > Thanks Guilliam. > > > I'm not sure you really addressed > https://externals.io/message/114561#114680 ? > > I thought I had solved this problem by implementing the > `RandomNumberGenerator` interface. > > Accepting strings and objects as arguments is certainly complicated, but I > thought it met the necessary requirements. > From the aspect of static analysis, there seems to be no problem. > > Reverting to a full object-based approach would increase the cost of > providing new RNGs from extensions. I think the string approach is superior > in this respect. > How much more "costly" would it be to define a class (implementing RandomNumberGenerator) and use its (full) name as the algo identifier? > Nikita's `createDefault()` approach is certainly good, but I think it is > still difficult for beginners to understand. > > I also thought about it for a while after that, and I think that the > current implementation is the most appropriate for now. What do you think? > I still agree with Nikita that there's a discrepancy between e.g. `new Random(RANDOM_XXX, $seed)` and `new Random(new CustomRNG(/*custom args*/))`, which also poses additional questions, e.g.: - (already pointed out by Jordi) `new Random(new CustomRNG(/*custom args*/), $seed)` is "illogical" (the passed $seed won't be used) but "possible" (even if your current implementation throws a ValueError when $seed is non-null); - This opens the possibility of "leaking" the RNG "source" (even if no `public function getRNG()`), e.g.: ``` $rng = new CustomRNG(/*custom args*/); $r1 = new Random($rng); $r2 = new Random($rng); var_dump($r1->nextInt() === $r2->nextInt()); // false (not true), I suppose? ``` or: ``` $rng = new CustomRNG(/*custom args*/); $r = new Random($rng); var_dump($r->nextInt()); // 1st of sequence $rng->generate(); var_dump($r->nextInt()); // 3rd of sequence (not 2nd), I suppose? ``` (possibly in less obvious ways), which may be considered bad or not (but still a discrepancy vs extension-provided algos). Again, taking a class name and optional extra args (or an $options array, like password_hash(), maybe even including 'seed' only for the algos that need one) to construct, rather than an already constructed object, might be better? But that would also "split" the constructor args from the class (increasing the risk of "mismatch"), and make using anonymous classes less easy, and maybe we haven't considered all the uses cases (e.g. what about `Random::getAlgoInfo(CustomRNG::class)`?)... So that might also be worse :/ It would be great to have more insights! (if nobody else speaks up then let's keep it as is of course) > > Couldn't the Random class simply implement `public function __clone(): > void` (that would internally do like `$this->rng = clone $this->rng;`)? > > Implicitly cloning in areas where the user cannot interfere may cause > problems when using objects that cannot be cloned. > However, this may be overthinking it. The reason is that a > `RandomNumberGenerator` that cannot be cloned should be implemented as algo > in the first place. > If there are no objections, I will change the implementation. > Ah, true, I hadn't thought about non-clonable implementations... But that's already the case for `__serialize()` and non-serializable implementations, right? (But let's wait a bit for possible objections, indeed) > > Indeed, a decision has to be made. If you throw an exception, we > wouldn't have the "issue" of different results on 32- vs 64-bit machines, > but XorShift128+ and Secure would be unusable on a 32-bit machine, right? > > I don't see any problem with the current implicit truncation behavior for > this. Even if a user switches to a 64-bit environment, compatibility can be > maintained by explicitly truncating the generated values. In addition, most > of the other methods only use 32bit internally. > If you are concerned about this, you should probably be concerned about > the incompatibility with MT_RAND_PHP. > That problem can also be compensated for with an extension that adds algo. > I thought you were "struggling with [this implicit truncation] behavior when calling nextInt() in a 32-bit environment using a 64-bit RNG [...] which means that the code loses compatibility with the result of running on a 64-bit machine"? And you asked if throwing an exception would be preferable? Anyway, I personally don't care about 32-bit (but other people may). > Regards, > Go Kudo > Regards, -- Guilliam Xavier --0000000000000c3d6a05c4578689--