rfc:random_extension_improvement
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:random_extension_improvement [2022/06/22 14:26] – update 1.5 zeriyoshi | rfc:random_extension_improvement [2022/07/22 09:55] (current) – reword two issues as the problem (VS the proposed solution) guilliamxavier | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Random Extension Improvement ====== | ====== PHP RFC: Random Extension Improvement ====== | ||
- | * Version: 1.5 | + | * Version: 1.5.4 |
* Date: 2022-06-16 | * Date: 2022-06-16 | ||
* Author: Go Kudo < | * Author: Go Kudo < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | There are several | + | Several |
- | + | ||
- | Key issues include: | + | |
==== Engine implementations are not final ==== | ==== Engine implementations are not final ==== | ||
- | The Random Extension has classes that are natively implemented as RNG engines, but they are not marked as final. | + | The Random Extension has classes that are natively implemented as RNG engines, but they are not marked as <php>final</ |
This allows classes to be created that inherit from native classes, but as stated in the previous RFC, user-implemented engines are inferior to native classes in terms of execution efficiency. This is true even for inheritance without method overrides, which often leads to confusion. | This allows classes to be created that inherit from native classes, but as stated in the previous RFC, user-implemented engines are inferior to native classes in terms of execution efficiency. This is true even for inheritance without method overrides, which often leads to confusion. | ||
- | The extension already provides a Random\Engine interface with a single generate(): string method. | + | The extension already provides a '' |
Even if the native classes are made final, it is easy for the user to create an alternative class using delegates. | Even if the native classes are made final, it is easy for the user to create an alternative class using delegates. | ||
This is clearly an API design error, and the native implementations of the interface should be marked as final. | This is clearly an API design error, and the native implementations of the interface should be marked as final. | ||
- | ==== Random\SerializableEngine is outdated | + | ==== Random\SerializableEngine is not useful |
- | This interface is a remnant from when the Serializable interface was still useful. This interface is no longer needed in PHP, as serializability is now determined by the existence of a magic method. | + | This interface is a remnant from when the <php>Serializable</ |
- | For this reason, remove | + | For this reason, remove |
This means that an Engine that implements SerializableEngine will no longer implement it. | This means that an Engine that implements SerializableEngine will no longer implement it. | ||
However, serializability is determined by the implementation of the magic method in current PHP, so it has no effect. | However, serializability is determined by the implementation of the magic method in current PHP, so it has no effect. | ||
- | ==== CombinedLCG is outdated | + | ==== Random\Engine\CombinedLCG is low quality |
- | The newly added Random\Engine\CombinedLCG is only for use with PHP's lcg_value() function. | + | The newly added '' |
However, this algorithm is very classical and the quality of the output random numbers is at the lowest level. | However, this algorithm is very classical and the quality of the output random numbers is at the lowest level. | ||
In order to preserve the implementation of the lcg_value() function, the internal implementation is retained, but the implementation as a class is being dropped to prevent users from unintentionally using it. | In order to preserve the implementation of the lcg_value() function, the internal implementation is retained, but the implementation as a class is being dropped to prevent users from unintentionally using it. | ||
- | ==== Add Randomizer:: | + | ==== There is no equivalent of array_rand() ==== |
- | array_rand() uses RNG internally, but there is no alternative method in Randomizer. | + | <php>array_rand()</ |
As per the previous RFC, this was the intent, but upon further investigation, | As per the previous RFC, this was the intent, but upon further investigation, | ||
- | So add a method | + | So add a method |
+ | |||
+ | It may look incompatible with <php>array_rand(array $array, int $num = 1): int|string|array</ | ||
<code php> | <code php> | ||
+ | $array = [' | ||
+ | |||
+ | // Before: | ||
mt_srand(1234, | mt_srand(1234, | ||
- | $beforeMultiple | + | $single |
- | $beforeSingle | + | $multiple |
+ | // After: | ||
$engine = new Random\Engine\Mt19937(1234, | $engine = new Random\Engine\Mt19937(1234, | ||
$randomizer = new Random\Randomizer($engine); | $randomizer = new Random\Randomizer($engine); | ||
- | $beforeMultiple | + | $single |
- | [$beforeSingle] = $randomizer-> | + | // or: [$single] = $randomizer-> |
+ | $multiple | ||
</ | </ | ||
==== " | ==== " | ||
- | In PHP, " | + | In PHP, " |
- | Therefore, it may be better to change the alternative method of str_shuffle(), | + | Therefore, it may be better to change the alternative method of str_shuffle(), |
- | ==== Refine | + | ==== Engine |
- | To make it more readable and regular, the class name is changed as follows: | + | To clearly identify the implemented algorithm, the PCG64 and MersenneTwister engines should be renamed to their canonical upstream |
- | * Random\Engine\PCG64 | + | * '' |
- | * Random\Engine\MersenneTwister | + | * '' |
==== PCG is not so famous ==== | ==== PCG is not so famous ==== | ||
PCG is a very good algorithm, boasting great randomness and performance. | PCG is a very good algorithm, boasting great randomness and performance. | ||
- | |||
However, I think its name recognition is in some ways inferior to Vigna' | However, I think its name recognition is in some ways inferior to Vigna' | ||
- | Therefore, I reimplement Xoshiro256**, | + | Therefore, I reimplement |
===== Proposal ===== | ===== Proposal ===== | ||
Line 83: | Line 87: | ||
==== Engine implementations are not final ==== | ==== Engine implementations are not final ==== | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
- | ==== Random\SerializableEngine is outdated | + | ==== Random\SerializableEngine is not useful |
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
- | ==== CombinedLCG is outdated | + | ==== Random\Engine\CombinedLCG is low quality |
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
- | ==== Add Randomizer:: | + | ==== There is no equivalent of array_rand() ==== |
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
Line 111: | Line 115: | ||
==== " | ==== " | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
</ | </ | ||
- | ==== Refine | + | ==== Engine |
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
Line 125: | Line 129: | ||
==== PCG is not so famous ==== | ==== PCG is not so famous ==== | ||
- | <doodle title=" | + | <doodle title=" |
* Yes | * Yes | ||
* No | * No | ||
Line 134: | Line 138: | ||
The following names have been reserved and will no longer be available: | The following names have been reserved and will no longer be available: | ||
+ | * Random\Engine\Mt19937 | ||
* Random\Engine\PcgOneseq128XslRr64 | * Random\Engine\PcgOneseq128XslRr64 | ||
* Random\Engine\Xoshiro256StarStar | * Random\Engine\Xoshiro256StarStar | ||
- | * Random\Engine\Mt19937 | ||
The following class names will be made available again: | The following class names will be made available again: | ||
* Random\Engine\CombinedLCG | * Random\Engine\CombinedLCG | ||
+ | * Random\Engine\MersenneTwister | ||
+ | * Random\Engine\PCG64 | ||
* Random\SerializableEngine | * Random\SerializableEngine | ||
Line 163: | Line 169: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | currently none | + | * https:// |
===== References ===== | ===== References ===== | ||
* https:// | * https:// |
rfc/random_extension_improvement.txt · Last modified: 2022/07/22 09:55 by guilliamxavier