Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2024

Conversation

TimWolla
Copy link
Member

With the removal of the backwards compatibility headers in #13108, breaking compatibility for extensions designed for PHP versions before 8.2, I'm using the opportunity to remove some more legacy warts of the internal API.

See the commit messages of the individual commits for more details regarding the reasoning for the removal of each piece of the API.

TimWolla added 4 commits May 23, 2024 22:04
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.
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.
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.
@TimWolla TimWolla requested a review from Girgias May 23, 2024 20:43
@TimWolla TimWolla requested a review from zeriyoshi as a code owner May 23, 2024 20:43
@TimWolla TimWolla merged commit 8cf8751 into php:master May 27, 2024
9 of 10 checks passed
@TimWolla TimWolla deleted the random-clean-internal-api branch May 27, 2024 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants