rfc:rng_extension

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
rfc:rng_extension [2022/02/14 10:42]
zeriyoshi update 4.0
rfc:rng_extension [2022/02/15 11:37]
zeriyoshi add stub, user-land limitations, open issues
Line 1: Line 1:
 ====== PHP RFC: Random Extension 4.0 ====== ====== PHP RFC: Random Extension 4.0 ======
-  * Version: 4.0+  * Version: 4.0.1
   * Date: 2022-02-14   * Date: 2022-02-14
   * Author: Go Kudo <zeriyoshi@gmail.com> <g-kudo@colopl.co.jp>   * Author: Go Kudo <zeriyoshi@gmail.com> <g-kudo@colopl.co.jp>
   * Status: Under Discussion   * Status: Under Discussion
-  * Implementation: https://github.com/php/php-src/pull/XXXX+  * Implementation: https://github.com/php/php-src/pull/8094
   * First Published at: http://wiki.php.net/rfc/object_scope_prng   * First Published at: http://wiki.php.net/rfc/object_scope_prng
  
Line 59: Line 59:
  
 These classes will hold independent RNG state and will not affect the global scope. These classes will hold independent RNG state and will not affect the global scope.
 +
 +RNGs other than XorShift128Plus are based on the RNGs currently implemented in PHP.
  
 An interface Random\NumberGenerator is also added and are implmeneted by the classes above.  An interface Random\NumberGenerator is also added and are implmeneted by the classes above. 
 +
 This interface has only a single generate() method which makes it possible to switch between RNG implementations depending on the situation, This interface has only a single generate() method which makes it possible to switch between RNG implementations depending on the situation,
-allowing alternative implementations to be done by PHP in userland. This is useful, for example, for running tests.+allowing alternative implementations to be done by PHP in userland. This is useful, for example, running tests.
  
-RNGs other than XorShift128Plus are based on the RNGs currently implemented in PHP.+<code php> 
 +final class FixedForUnitTest implements \Random\NumberGenerator 
 +
 +    private int $count = 0; 
 +     
 +    public function generate(): int 
 +    { 
 +        return ++$this->count; 
 +     } 
 +
 +</code> 
 + 
 +However, the width of the random number that can be generated by a userland implementation depends on the size of the int in PHP on that platform. This means that you can only generate up to 32 bits in a 32-bit environment, and up to 64 bits in a 64-bit environment. This likewise means that 32-bit RNGs cannot be implemented in userland in a 64-bit environment. 
 + 
 +<code php> 
 +// on 64-bit machine 
 + 
 +final class UserMersenneTwister extends \Random\NumberGenerator\MersenneTwister 
 +
 +    // uses 64-bit internally, if generated: 1 (zend_long), bits: 0000000000000000000000000000000000000000000000000000000000000001 (64-bit) 
 +    // normally MersenneTwister bits: 00000000000000000000000000000001 (32-bit) 
 +    public function generate(): int 
 +    { 
 +        return parent::generate() - 1; 
 +     } 
 +
 +</code> 
 + 
 +I don't think this is a problem, as most requests to generate random numbers in userland are likely to return a fixed value or reproduce a specific scenario.
  
-The Random\Randomizer class will be added to manipulate data using these RNGs. +Random\Randomizer class will be added to manipulate data using these RNGs. 
  
 This class provides the following methods: This class provides the following methods:
Line 77: Line 108:
  
 Method equivalent to array_rand() was not implemented at this time because the implementation is complex and can be easily implemented in userland if necessary. Method equivalent to array_rand() was not implemented at this time because the implementation is complex and can be easily implemented in userland if necessary.
 +
 +The stubs of functionality provided by this extension are as follows:
 +
 +https://github.com/colopl/php-src/blob/upstream_rfc/scoped_rng_for_pr/ext/random/random.stub.php
  
 Examples of these uses are as follows: Examples of these uses are as follows:
Line 101: Line 136:
 $randomizer = new Random\Randomizer(new Random\NumberGenerator\MersenneTwister(1234, MT_RAND_PHP)); $randomizer = new Random\Randomizer(new Random\NumberGenerator\MersenneTwister(1234, MT_RAND_PHP));
 foobar(); foobar();
-$result = $randomizer->stringShuffle('foobar');+$result = $randomizer->shuffleString('foobar');
 </code> </code>
  
Line 178: Line 213:
  
 ===== Open Issues ===== ===== Open Issues =====
-none+ 
 +=== It is not possible to reproduce 32-bit Mersenne Twister in userland in a 64-bit environment === 
 + 
 +https://github.com/php/php-src/pull/8094#pullrequestreview-881660425 
 + 
 +Currently, the width of PHP's NumberGenerator::generate() generation is implicitly assumed to be zend_long (the size of an int in PHP). 
 +This means that it is not possible to implement a RNG with a generation width other than 64-bit width. 
 + 
 +However, this RFC assumes that userland RNG implementations are often only used to reproduce certain scenarios in tests, and I personally think that this is sufficient. 
 + 
 +=== Should generate() really return a number? === 
 + 
 +Tim says NumberGenerator::generate() should return a string instead of an int. 
 + 
 +https://externals.io/message/117026#117032 
 + 
 +While it is true that returning a string allows for more flexibility in the range of RNG generation, it poses a convenience problem, In particular, it makes it difficult to implement a userland RNG to reproduce a particular scenario. 
 + 
 +=== Is adopting XorShift128Plus a good choices? === 
 + 
 +As mentioned in the Internals ML, there are a few known issues with XorShift128+. 
 + 
 +https://prng.di.unimi.it/ 
 + 
 +https://externals.io/message/117026#117030 
 + 
 +May need to consider a better candidate as an RNG to add.
  
 ===== Vote ===== ===== Vote =====
Line 189: Line 250:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-  * https://github.com/php/php-src/pull/XXXX+  * https://github.com/php/php-src/pull/8094
rfc/rng_extension.txt · Last modified: 2022/08/01 16:52 by timwolla