doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Error in documentation for rand()

Open ohyeaah opened this issue 1 year ago • 4 comments

In the notes section of rand() it says:

(max - min) <= getrandmax()

But must be

abs(max - min) <= getrandmax()

ohyeaah avatar Oct 22 '24 07:10 ohyeaah

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

cmb69 avatar Oct 22 '24 11:10 cmb69

@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 reverse order. See php/php-src@9a9004318fa3a5b2c2c3c4a4fafed9ad4767df4a. 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 avatar Oct 22 '24 11:10 TimWolla

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().

TimWolla avatar Oct 22 '24 11:10 TimWolla

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 avatar Oct 22 '24 11:10 cmb69