-
Notifications
You must be signed in to change notification settings - Fork 7.8k
srand(0) set random seed to 0 instead random #10292
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
Comments
<?php
function foo()
{
srand(0);
$bar = rand(0, 1023);
return $bar;
}
var_dump(foo() === foo());
?> From php >= 4.3.0 expected result bool true |
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. |
The docs mention that Anyhow, the problem is that the stubs are not matching the implementation; the stubs claim the default of 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: Line 691 in 4427b2e
So there is no default value. So back to php-src. Documentation should be fixed afterwards. |
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? |
Ah, and since this is now part of ext/random, maybe @zeriyoshi and @TimWolla have thoughts on this as well. |
I do have thoughts on this: The only good reason to use I suggest to change the actual default value to |
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 As for me
looks more intuitive. For example
|
Also:
Also reproduces this bug, although it looks like I didn't pass $seed as argument. I understand why this is happening, I just think that |
Seeds the random number generator with seed or with a random value if no seed is given.
Note* MT_RAND_PHP This mode is available for backward compatibility. function foo(): int
{
srand(seed: MT_RAND_MT19937);
$bar = rand(0, 1023);
var_dump($bar);
return $bar;
}
var_dump(foo() === foo()); // Range Between 0 and 1023 with seed 0 is return integer value 684 |
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 |
Everything should work as written in the documentation. Now we have some secret/hidden/undocumented function overloading feature over default arguments feature. |
Unless the documentation is in error. |
Oh, I thought the documentation was correct and this bug appeared version update. |
No the documentation should reflect the actual implementation in this case. So the issue is more related to the docs lying currently. |
I agree with making the default value |
…d mt_srand() nullable (#10380) Co-authored-by: Tim Düsterhus <[email protected]>
- PHP-8.1: Fix GH-10292 1st param of mt_srand() has UNKNOWN default on PHP <8.3
- PHP-8.1: Fix GH-10292 1st param of mt_srand() has UNKNOWN default on PHP <8.3
Description
The following code:
Resulted in this output:
But I expected this output instead:
But this code:
Works fine:
https://www.php.net/manual/en/function.srand.php
It's seems like srand(0) process 0 as new seed value instead 0 must reset seed to new random value.
PHP Version
PHP 8.2.0
Operating System
Win 11 build 25272 (insider)
The text was updated successfully, but these errors were encountered: