rfc:random_extension_improvement

This is an old revision of the document!


PHP RFC: Random Extension Improvement

Introduction

There are several issues with RFC: Random Extension 5.x that are already in the voting phase due to lack of proper discussion.

Key issues include:

Engine implementations are not final

The Random Extension has classes that are natively implemented as RNG engines, but they are not marked as 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.

The extension already provides a Random\Engine interface with a single generate(): string method. 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.

Random\SerializableEngine is outdated

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.

For this reason, remove RandomEngine\SerializableEngine.

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.

CombinedLCG is outdated

The newly added Random\Engine\CombinedLCG is only for use with PHP's lcg_value() function. 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.

Add Randomizer::pickArrayKeys(array $array, int $num): array method

array_rand() uses RNG internally, but there is no alternative method in Randomizer. As per the previous RFC, this was the intent, but upon further investigation, array_rand() is used by many packages and should probably be drop-in replaceable.

So add a method called Randomizer::pickArrayKeys(array $array, int $num): array. It looks incompatible with array_rand(), but you can get completely consistent results by doing the following:

mt_srand(1234, MT_RAND_PHP);
$beforeMultiple = array_rand(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], 2); // (array) ['bar', 'baz']
$beforeSingle = array_rand(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], 1); // (string) foo
 
$engine = new Random\Engine\Mt19937(1234, MT_RAND_PHP);
$randomizer = new Random\Randomizer($engine);
$beforeMultiple = $randomizer->pickArrayKeys(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], 2); // (array) ['bar', 'baz']
[$beforeSingle] = $randomizer->pickArrayKeys(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], 1); // (string) foo

"string" means a binary

In PHP, “string” means a binary. This is often a problem when using multibyte characters. For example, str_shuffle on a Japanese (UTF-8) string will give messed up results.

Therefore, it may be better to change the alternative method of str_shuffle(), Randomizer::shuffleString(), to Randomizer::shuffleBytes(). This is a more appropriate name.

Refine classnames

To make it more readable and regular, the class name is changed as follows:

  • Random\Engine\PCG64 -> Random\Engine\PcgOneseq128XslRr64
  • Random\Engine\MersenneTwister -> Random\Engine\Mt19937

PCG is not so famous

PCG is a very good algorithm, boasting great randomness and performance.

However, I think its name recognition is in some ways inferior to Vigna's RNG, which started with Xorshift.

Therefore, I reimplement Xoshiro256**, which was previously mentioned as a candidate, and create the class Random\Engine\Xoshiro256StarStar. This will avoid the problem of using the old MT19937 since it is not familiar with PCG.

Proposal

For each of these issues, we will create a ballot option and make a decision.

Engine implementations are not final

Engine implementations to final
Real name Yes No
Final result: 0 0
This poll has been closed.

Random\SerializableEngine is outdated

Remove Random\SerializableEngine
Real name Yes No
Final result: 0 0
This poll has been closed.

CombinedLCG is outdated

Drop Random\Engine\CombinedLCG
Real name Yes No
Final result: 0 0
This poll has been closed.

Add Randomizer::pickArrayKeys(array $array, int $num): array method

Add Random\Randomizer::pickArrayKeys(array $array, int $num): array
Real name Yes No
Final result: 0 0
This poll has been closed.

"string" means a binary

Rename Random\Randomizer::shuffleString() to Random\Randomizer::shuffleBytes()
Real name Yes No
Final result: 0 0
This poll has been closed.

Refine classnames

Change classnames
Real name Yes No
Final result: 0 0
This poll has been closed.

PCG is not so famous

Implement Random\Engine\Xoshiro256StarStar
Real name Yes No
Final result: 0 0
This poll has been closed.

Backward Incompatible Changes

The following names have been reserved and will no longer be available:

  • Random\Engine\PcgOneseq128XslRr64
  • Random\Engine\Xoshiro256StarStar
  • Random\Engine\Mt19937

The following class names will be made available again:

  • Random\Engine\CombinedLCG
  • Random\SerializableEngine

Proposed PHP Version(s)

8.2

RFC Impact

To SAPIs

none

To Existing Extensions

none

To Opcache

none

New Constants

none

php.ini Defaults

none

Patches and Tests

currently none

References

rfc/random_extension_improvement.1655907968.txt.gz · Last modified: 2022/06/22 14:26 by zeriyoshi