Skip to content

Commit 8cf8751

Browse files
authored
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
1 parent d4839b9 commit 8cf8751

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

UPGRADING.INTERNALS

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ PHP 8.4 INTERNALS UPGRADE NOTES
176176
- The macro RAND_RANGE_BADSCALING() has been removed. The implementation
177177
should either be inlined and undefined behavior fixed or it should be
178178
replaced by a non-biased scaler.
179+
- The php_srand() and php_rand() functions have been removed. These were
180+
slim wrappers around the corresponding php_mt_srand() and php_mt_rand()
181+
function since PHP 7.1, but using zend_long instead of uint32_t as their
182+
input/output types. This made their behavior incompatible between 32-bit
183+
and 64-bit builds of PHP. Users of these functions are encouraged to
184+
migrate to one of the more modern engines provided since PHP 8.2. If that
185+
is not possible, due to backwards compatibility requirements, then the
186+
php_mt_srand() and php_mt_rand() functions should be called directly and
187+
the values appropriately casted.
188+
- The PHP_RAND_MAX and RAND_MAX constants corresponding to the removed
189+
php_rand() have also been removed.
179190
- The generate member of a php_random_algo is now expected to return
180191
the new php_random_result struct, replacing the last_generated_size
181192
member of the php_random_status struct and the generate_size member of

ext/random/php_random.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ PHPAPI uint32_t php_mt_rand(void);
5858
PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max);
5959
PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max);
6060

61-
# ifndef RAND_MAX
62-
# define RAND_MAX PHP_MT_RAND_MAX
63-
# endif
64-
65-
# define PHP_RAND_MAX PHP_MT_RAND_MAX
66-
67-
PHPAPI void php_srand(zend_long seed);
68-
PHPAPI zend_long php_rand(void);
69-
7061
typedef struct _php_random_status_state_combinedlcg {
7162
int32_t state[2];
7263
} php_random_status_state_combinedlcg;

ext/random/random.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,6 @@ PHPAPI zend_long php_mt_rand_common(zend_long min, zend_long max)
446446
}
447447
/* }}} */
448448

449-
/* {{{ php_srand */
450-
PHPAPI void php_srand(zend_long seed)
451-
{
452-
php_mt_srand((uint32_t) seed);
453-
}
454-
/* }}} */
455-
456-
/* {{{ php_rand */
457-
PHPAPI zend_long php_rand(void)
458-
{
459-
return php_mt_rand();
460-
}
461-
/* }}} */
462-
463449
/* {{{ Returns a value from the combined linear congruential generator */
464450
PHP_FUNCTION(lcg_value)
465451
{

0 commit comments

Comments
 (0)