Hello Nikita.
> In addition to the functions you already list, I'd add
> function rng_bytes(RNGInterface $rng, int $length): string;
> function rng_between(RNGInterface $rng, int $min, int $max): int;
Certainly these are useful. I've added this to the RFC in TypeC.
> Just to share another possibility, we could also modify existing
functions to optionally accept RNGInterface, like
> shuffle(array &$array, ?RNGInterface $rng = null): bool;
This approach may be appropriate. This way, the user is implicitly aware
that he is using a global state RNG.
> What do you mean by "use unintended instances" here?
For example, if you write the following code, the user will receive an
unintended result.
```php
$rng1 = new \RNG\XorShift128Plus(1234);
$rng2 = new \RNG\MT19937(1234);
$arr1 = range(1, 100);
$arr2 = range(1, 100);
rng_shuffle($rng1, $arr1);
rng_shuffle($rng1, $arr2); // oops, this case uses $rng1 RNG instance.
```
I believe that if these features are provided by class methods, such
mistakes can be avoided.
However, it is a matter of likelihood and similar mistakes can happen
anyway.
> Regarding implementation complexity, yes, this version is a bit more
involved if we want to allow userland implementations of RNGInterface. To
be honest I don't particularly care about supporting userland (we could
also only allow it to be implemented internally), but I think it shouldn't
be too hard either.
Yes, it's probably possible. I need to understand the PHP implementation
more deeply :)
If you know of any implementations that might be helpful, I would like to
know about them.
As for the implementation of userland, I don't put that much emphasis on it
either. I just use classes for clarity, and I'm fine only provided by the
core or extensions.
> This is generally the part I'm least sure about. I agree that it would be
good to leverage 64-bit numbers on systems that support them. On the other
hand, I also think that having a consistent random number stream between
32-bit and 64-bit systems may be important. For example, if you use this
functionality for predictable "randomness" in unit tests, then it would be
a problem if your library could only be tested on 32-bit or only on 64-bit.
Certainly, this seems to be a problem. However, the 32bit architecture
environment is very limited, and a 64bit RNG can be useful for applications
and libraries that are designed to run on 64bit architecture.
Also, some of the new RNGs that we are trying to implement, such as
XorShift128+, have an internal state of uint64_t. The random numbers they
generate are always 64bit, and rounding them to 32bit may prevent their
intended use.
For these reasons, I think that we should provide a `next64()` method.
However, care must be taken in serializing the instance. We are trying to
implement 32bit environment-safe serialization in the current PR, but it is
not smart to say the least.
https://github.com/php/php-src/pull/6568/files#diff-0f1c13e606f11fb5da8dc091109852a6dec328e5aea2389091ff839510cf23faR275
Based on the above discussion, I have added TypeC2 to the RFC.
How about this?
https://wiki.php.net/rfc/object_scope_prng
Regards,
Go Kudo
2021年1月6日(水) 19:50 Nikita Popov <nikita.ppv@gmail.com>: