Skip to content
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

Error in documentation for rand() #3890

Open
ohyeaah opened this issue Oct 22, 2024 · 4 comments
Open

Error in documentation for rand() #3890

ohyeaah opened this issue Oct 22, 2024 · 4 comments
Labels
bug Documentation contains incorrect information

Comments

@ohyeaah
Copy link

ohyeaah commented Oct 22, 2024

In the notes section of rand() it says:

(max - min) <= getrandmax()

But must be

abs(max - min) <= getrandmax()

@cmb69
Copy link
Member

cmb69 commented Oct 22, 2024

Oh, indeed you can do rand(10, 1) etc. @TimWolla, is this deliberate?

@TimWolla
Copy link
Member

@cmb69 It's complicated. The short answer is: rand() really shouldn't exist any longer (https://wiki.php.net/rfc/deprecations_php_8_3#global_mersenne_twister).

Medium answer: Yes, for compatibility.

The long answer is that rand() is effectively an alias for mt_rand() since 7.1, with the exception of accepting $min and $max in reserve order. See php/php-src@9a90043. And whether that was intentionally supported in the first place is not clear. It appears to be a side effect of the implementation of the biased scaler used before PHP 7.2.

@TimWolla
Copy link
Member

Unfortunately the documentation still needs to document PHP 7.x, otherwise the page for rand() could simply be merged with that one for mt_rand(). Or be made a stub, similarly to sizeof().

@cmb69
Copy link
Member

cmb69 commented Oct 22, 2024

Thanks for the clarification @TimWolla! (I've only had looked at rand() and php_mt_rand_common().)

I think documenting rand() as alias of mt_rand() makes sense; we should document the few differences, though.

PS: analogous for srand()

@cmb69 cmb69 added the bug Documentation contains incorrect information label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation contains incorrect information
Projects
None yet
Development

No branches or pull requests

3 participants