You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
srand(0) set random seed to 0 instead random #10292
The srand page is a bit weird because it seems to try to document a function that doesn't exist anymore: it's mt_srand now, and that does not do the "with a random value if seed is 0" behavior. And given that 3v4l, it seems the original rand didn't actually do this?
The srand page is a bit weird because it seems to try to document a function that doesn't exist anymore: it's mt_srand now
The docs mention that srand() is an alias of mt_srand() as of PHP 7.1.0, and since the docs are currently supposed to document PHP 7.0 - 8.2, that looks okay.
Anyhow, the problem is that the stubs are not matching the implementation; the stubs claim the default of $seed would be zero:
The big question is how to solve this: fix the stubs (resulting in a painful signature), or fix the implementation (resulting in a BC break). @kocsismate, thoughts?
I do have thoughts on this: The only good reason to use mt_rand() and mt_srand() in the first place since PHP 7.0 is getting a reproducible sequence, thus changing the long-standing behavior of 0 is a no-go in my book, especially since this would either also affect the OOP Mt19937 engine or result in differences between Mt19937 and mt_rand, which directly goes against the intended behavior.
I suggest to change the actual default value to null, like Mt19937's default is null as well and make null equivalent to a random uint32_t.
Seeds the random number generator with seed or with a random value if no seed is given.
Ok, but it factually contradicts to signature of this function:
mt_srand(int $seed = 0, int $mode = MT_RAND_MT19937): void
Where default value of $seed is 0. And I expect mt_srand(); and mt_srand(0); must works identically. For example, if I declare some function foo(int $ i = 0) then foo(); and foo(0); works identically.
As for me
srand(?int $seed = null, int $mode = MT_RAND_MT19937): void
looks more intuitive. For example substr takes int or null:
substr(string $string, int $offset, ?int $length = null): string
I think this is indeed be the best way forward, although I'm slightly concerned regarding coercive typing; prior to PHP 8.1.0, one could pass null as $seed which would be coerced to zero. However, the alternative of changing the default to UNKNOWN would prevent any automatic seeding when the $mode parameter would be used.
I agree with making the default value null, but I think we should only fix this in master due to the BC break, shouldn't we? The BC break is very unfortunate, but we changed quite many similar signatures in the past, so this shouldn't be too big of an issue, especially because the documentation clearly states that the parameter is an int right now, and the description is correct about when the automatic seed is applied (when no parameter is given).
Activity
hormus commentedon Jan 11, 2023
From php >= 4.3.0 expected result bool true
mt_srand(0) From php >= 7.1.0 expected result bool true
https://3v4l.org/JNHF8
damianwadley commentedon Jan 11, 2023
The srand page is a bit weird because it seems to try to document a function that doesn't exist anymore: it's mt_srand now, and that does not do the "with a random value if seed is 0" behavior. And given that 3v4l, it seems the original rand didn't actually do this?
Either way this seems like a documentation issue.
cmb69 commentedon Jan 11, 2023
The docs mention that
srand()is an alias ofmt_srand()as of PHP 7.1.0, and since the docs are currently supposed to document PHP 7.0 - 8.2, that looks okay.Anyhow, the problem is that the stubs are not matching the implementation; the stubs claim the default of
$seedwould be zero:php-src/ext/random/random.stub.php
Line 19 in 4427b2e
However, actually the implementation just checks whether an argument has been passed, or not:
php-src/ext/random/random.c
Line 691 in 4427b2e
So there is no default value.
So back to php-src. Documentation should be fixed afterwards.
cmb69 commentedon Jan 11, 2023
The big question is how to solve this: fix the stubs (resulting in a painful signature), or fix the implementation (resulting in a BC break). @kocsismate, thoughts?
cmb69 commentedon Jan 11, 2023
Ah, and since this is now part of ext/random, maybe @zeriyoshi and @TimWolla have thoughts on this as well.
TimWolla commentedon Jan 11, 2023
I do have thoughts on this: The only good reason to use
mt_rand()andmt_srand()in the first place since PHP 7.0 is getting a reproducible sequence, thus changing the long-standing behavior of0is a no-go in my book, especially since this would either also affect the OOP Mt19937 engine or result in differences between Mt19937 and mt_rand, which directly goes against the intended behavior.I suggest to change the actual default value to
null, like Mt19937's default isnullas well and makenullequivalent to a random uint32_t.adic3x commentedon Jan 11, 2023
https://www.php.net/manual/en/function.mt-srand.php
Ok, but it factually contradicts to signature of this function:
Where default value of $seed is 0. And I expect
mt_srand();andmt_srand(0);must works identically. For example, if I declare somefunction foo(int $ i = 0)thenfoo();andfoo(0);works identically.As for me
looks more intuitive. For example
substrtakes int or null:6 remaining items
Girgias commentedon Jan 12, 2023
No the documentation should reflect the actual implementation in this case. So the issue is more related to the docs lying currently.
kocsismate commentedon Jan 19, 2023
I agree with making the default value
null, but I think we should only fix this in master due to the BC break, shouldn't we? The BC break is very unfortunate, but we changed quite many similar signatures in the past, so this shouldn't be too big of an issue, especially because the documentation clearly states that the parameter is anintright now, and the description is correct about when the automatic seed is applied (when no parameter is given).Fix GH-10292 make the default value of the first parame of srand() an…
Fix phpGH-10292 1st param of mt_srand() has UNKNOWN default on PHP <8.3
Fix GH-10292 1st param of mt_srand() has UNKNOWN default on PHP <8.3
Merge branch 'PHP-8.1' into PHP-8.2
Merge branch 'PHP-8.2'