Re: [RFC] [Draft] Add Randomizer class (before: Add RNG extension)
| From: | Guilliam Xavier | Date: | Wed, 26 May 2021 10:16:37 +0000 |
| Subject: | Re: [RFC] [Draft] Add Randomizer class (before: Add RNG extension) | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-114608@lists.php.net to get a copy of this message | ||
On Tue, May 25, 2021 at 5:36 PM Go Kudo <zeriyoshi@gmail.com> 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