Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114608 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60200 invoked from network); 26 May 2021 10:05:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 May 2021 10:05:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C0E321804B0 for ; Wed, 26 May 2021 03:16:49 -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-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 ; Wed, 26 May 2021 03:16:49 -0700 (PDT) Received: by mail-lf1-f48.google.com with SMTP id f30so1728179lfj.1 for ; Wed, 26 May 2021 03:16:49 -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=Hai74D+J9gXOVRE+dvAd/YgppQoBe7jpxMeGlosftSE=; b=h8w88P+sLJLYRxFgE1c3ES30L9f7ywRaXgoI4FbiNoKl7LoJSvNOribt7Fq+nMXfT2 iPiYWQULvMc24OGEYJo7LMQlrMk0yWfqz09Z+Iuvxo3jClDeuX4QK29Y64+NNiuDRtL3 DwxDmllY5NKm2syd2csSUgjk8j5m2/Hb0+d5Ji+x7Y107ZVunT1C7TSRz13ht0SraIEy Mh5yOmB1owxBH3pPLG2MRJeq9rMFP1W9Z6iN/QOAjv7N3BGXA4GY53tMXzIiI2QwboPc zozamomUtF7MLF0gdPn6LUKuhlOfVM0PJ94vEq3J15Z3TSkSYCuouMP9IH6A6VrP4M4C QX+Q== 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=Hai74D+J9gXOVRE+dvAd/YgppQoBe7jpxMeGlosftSE=; b=OhlWN+dX2JQXVuaIuwedoneczf4n4/zhVVUFHibEAw4Yczx3kY+OjiNSAnxEIYmq7F bY83JPQDrLrpT7zWIBgpjMVyDTZp5T/pN3fNSjms+XL0QK87aPpQjS5wHsakOyyQOtFF eABa6Hw3FkHAU3E2h42/+P5QM9Ynz8+BI2V20H6pwN/M9ePPZt4NR7Zq/DvK8H5B/Dsj Hlgd9XxKLGWFotdiq3xJhPvaNkJPyo+krzqyeLHemAF2u+0nPlkmK9LVMGb9sWUbmFok 7A1BirRD9tgEYEDuQ+eTnHBIgXKsJCpnEkckLQiZ1oVzedJ7xQA/d54FWYaNGrNEM5/u dOxQ== X-Gm-Message-State: AOAM533/wXkuJ7gRoocsbQ0w6XrYs/bRNZED2Kj8mIGWvPupx6xIaGrE abacEo+9H9YsWwN/lfsDxxY7vU17HVHNaupNKg== X-Google-Smtp-Source: ABdhPJypnLRjca34EK7aga3WLam+sJz/NgTpCsLC9/3upuaD235ICQchHYSN9Ht7Ccjzj6ETo6A4qLktPRk7JOD8uxg= X-Received: by 2002:a05:6512:1150:: with SMTP id m16mr1620023lfg.486.1622024206377; Wed, 26 May 2021 03:16:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 26 May 2021 12:16:37 +0200 Message-ID: To: Go Kudo Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000a3653705c338f048" Subject: Re: [RFC] [Draft] Add Randomizer class (before: Add RNG extension) From: guilliam.xavier@gmail.com (Guilliam Xavier) --000000000000a3653705c338f048 Content-Type: text/plain; charset="UTF-8" On Tue, May 25, 2021 at 5:36 PM Go Kudo wrote: > Hi, Thanks for the response. > > The RFC has been revised based on the points you pointed out. > > https://wiki.php.net/rfc/rng_extension > Thanks, it looks like you have addressed all previous points (for me at least). But also introduced a new one ;) with the new `static function getNonBiasedMax(string $algo): int`... (Note: I think some questions below could be answered by the list in general, not only Go Kudo.) Let's compare these two equivalent functions: function f1(int $seed): void { mt_srand($seed); $a = mt_rand(); $b = mt_rand(); var_dump($a, $b); } function f2(int $seed): void { $random = new Random(RANDOM_MT19937, $seed); $max = Random::getNonBiasedMax(RANDOM_MT19937); $a = $random->getInt(0, $max); $b = $random->getInt(0, $max); var_dump($a, $b); } In particular, note that we did *not* need to write the explicit/long version of f1: function f1_explicit(int $seed): void { mt_srand($seed); $max = mt_getrandmax(); $a = mt_rand(0, $max); $b = mt_rand(0, $max); var_dump($a, $b); } But what would happen with the implicit/short version of f2? function f2_implicit(int $seed): void { $random = new Random(RANDOM_MT19937, $seed); $a = $random->getInt(); $b = $random->getInt(); var_dump($a, $b); } Would we get "biased" results (by the way, what does that mean exactly)? like `mt_rand(PHP_INT_MIN, PHP_INT_MAX)`? Couldn't the default min/max be made "safe"? or maybe "dynamic" depending on the algo/implementation? Also, let's consider this: function g(Random $random): void { $max = /* ??? */; $a = $random->getInt(0, $max); $b = $random->getInt(0, $max); var_dump($a, $b); } Here, how to get the "non-biased max" for this Random instance (unknown algo)? Moreover, what would `Random::getNonBiasedMax(RANDOM_USER)` return? I think we would rather want/need to call e.g. `FixedNumberForTest::getNonBiasedMax()` (potentially overridden)? Maybe you could add a (non-static) `function getAlgo(): string`, so we could at least call `$random::getNonBiasedMax($random->getAlgo())`? (maybe it could also be more generally useful, possibly along with a `getSeed()`, akin to `password_get_info(string $hash)`?) or a non-static `function getNonBiasedMax(): int`, and rename the static one? (or even drop it after all? how often will we need it without having an instance? and if needed, is `new Random($algo, 0)` a costly operation?) or some other solution someone can think of? Ah that made me think: should some methods better be `final`? Finally, the current "Open Issues" section should probably renamed to "Discussion" or even "FAQ" here? Regards, -- Guilliam Xavier --000000000000a3653705c338f048--