1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

random: Remove internal aliases for the global Mt19937 functionality (#14314)

* random: Remove `php_rand()`

This effectively is just a slim wrapper around `(zend_long)php_mt_rand()`. It
is not compatible between 32-bit and 64-bit builds of PHP, due to the use of
`zend_long`, which may result in negative integersbeing returned on 32-bit
platforms, whereas 64-bit platforms will be compatible with `php_mt_rand()`. An
example would be the `0` seed, which emits 2357136044 on 64-bit platforms and
-1937831252 on 32-bit platforms.

Users of `php_rand()` should ideally migrate to one of the more modern engines,
with extension-specific state. If drop-in compatibility is desired, they can
just cast the result of `php_mt_rand()`. But providing it out of the box does
not provide a value-add and is potentially dangerous.

* random: Remove `php_srand()`

With `php_rand()` gone, preserving its companion `php_srand()` is just
confusing. The same recommendations apply: Migrate to a modern engine if
possible and just call `php_mt_srand()` with an appropriately casted input.

* random: Remove `PHP_RAND_MAX` and `RAND_MAX`

These are the companions to `php_rand()`, which was removed in a previous
commit.

Generally speaking the maximum returnable value is not particularly useful
anyways. Attempting it to create a random float by dividing the returned
integer by the maximum value would result in a bias if the maximum value would
be larger than 2**53 and even for that case, the various `range()` helpers
allow to easily retrieve a uniformly distributed integer from a suitable range.

* UPGRADING.INTERNALS
This commit is contained in:
Tim Düsterhus
2024-05-27 08:12:13 +02:00
committed by GitHub
parent d4839b96c2
commit 8cf8751533
3 changed files with 11 additions and 23 deletions

View File

@@ -176,6 +176,17 @@ PHP 8.4 INTERNALS UPGRADE NOTES
- The macro RAND_RANGE_BADSCALING() has been removed. The implementation
should either be inlined and undefined behavior fixed or it should be
replaced by a non-biased scaler.
- The php_srand() and php_rand() functions have been removed. These were
slim wrappers around the corresponding php_mt_srand() and php_mt_rand()
function since PHP 7.1, but using zend_long instead of uint32_t as their
input/output types. This made their behavior incompatible between 32-bit
and 64-bit builds of PHP. Users of these functions are encouraged to
migrate to one of the more modern engines provided since PHP 8.2. If that
is not possible, due to backwards compatibility requirements, then the
php_mt_srand() and php_mt_rand() functions should be called directly and
the values appropriately casted.
- The PHP_RAND_MAX and RAND_MAX constants corresponding to the removed
php_rand() have also been removed.
- The generate member of a php_random_algo is now expected to return
the new php_random_result struct, replacing the last_generated_size
member of the php_random_status struct and the generate_size member of

View File

@@ -58,15 +58,6 @@ PHPAPI uint32_t php_mt_rand(void);
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
# ifndef RAND_MAX
# define RAND_MAX PHP_MT_RAND_MAX
# endif
# define PHP_RAND_MAX PHP_MT_RAND_MAX
PHPAPI void php_srand(zend_long seed);
PHPAPI zend_long php_rand(void);
typedef struct _php_random_status_state_combinedlcg {
int32_t state[2];
} php_random_status_state_combinedlcg;

View File

@@ -446,20 +446,6 @@ PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
}
/* }}} */
/* {{{ php_srand */
PHPAPI void php_srand(zend_long seed)
{
php_mt_srand((uint32_t) seed);
}
/* }}} */
/* {{{ php_rand */
PHPAPI zend_long php_rand(void)
{
return php_mt_rand();
}
/* }}} */
/* {{{ Returns a value from the combined linear congruential generator */
PHP_FUNCTION(lcg_value)
{