rfc:random_ext

PHP RFC: Move to RNG functions into ext/random

Note

This RFC has been moved to Decline, as it is no longer needed due to changes in the proposal.

Introduction

This is due to the historical background, starting with lcg.c / php_lcg.h, the first implementation of the linear congruential generator (LCG), followed by rand.c / php_rand.c, which calls the libc random number implementation, and the Mersenne Twister implementation. mt_rand.c / php_mt_rand.h, an implementation of the Mersenne Twister, and random.c / php_random.h, a CSPRNG implementation, respectively, in the ext/standard extension.

The rand.c / php_rand.h to call the libc implementation is no longer relevant due to its alias to the Mersenne Twister in PHP 7.1, and is left for compatibility only.

RNGs are a frequently used feature in various implementations, and it is suggested that RNG-related functionality be carved out of the ext/standard extension and kindly made into an ext/random extension to reduce complexity and allow for future extensibility.

Proposal

Create a new ext/random extension and move all the following functions of the ext/standard extension.

Userland Functions:

  • lcg_value()
  • srand()
  • rand()
  • mt_srand()
  • mt_rand()
  • random_int()
  • random_bytes()

Internal APIs:

  • php_random_int_throw()
  • php_random_int_silent()
  • php_combined_lcg()
  • php_srand()
  • php_rand()
  • php_mt_srand()
  • php_mt_rand()
  • php_mt_rand_range()
  • php_mt_rand_common()
  • php_random_bytes()
  • php_random_int()

These features are provided in a single random.c / php_random.h, and ext/random is always bundled with all builds of PHP, as is ext/standard.

In addition, header files containing the following contents will continue to be provided for extensions and downstream projects that previously depended on ext/standard features. These are a transitional measure until the next major version and will be removed in the future.

  • standard/php_lcg.h
  • standard/php_rand.h
  • standard/php_mt_rand.h
  • standard/php_random.h

Backward Incompatible Changes

none

Proposed PHP Version(s)

PHP 8.2

RFC Impact

To SAPIs

none

To Existing Extensions

Will need to change the including header file.

To Opcache

none

New Constants

none

php.ini Defaults

none

Open Issues

none

Patches and Tests

Implementation

References

rfc/random_ext.txt · Last modified: 2022/02/08 07:31 by zeriyoshi