note 129127 added to function.srand
| From: | contact at einenlum dot com | Date: | Wed, 27 Dec 2023 15:52:27 +0000 |
| Subject: | note 129127 added to function.srand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221081@lists.php.net to get a copy of this message | ||
Keep in mind that now srand is an alias for mt_srand, but they behaved differently before. This
means you should not follow the documentation of srand, but the one of mt_srand, when using srand.
To reset the seed to a random value,
mt_srand(0) (or srand(0))
doesn't work. It sets the seed to 0. To reset the seed to a random value you must use
mt_srand() (or srand()).
<?php
$arr = [0, 1, 2, 3, 4];
srand(1); // or mt_srand(1) as they are now aliases
$keys = array_rand($arr, 2); // not random as expected
srand(0); // or mt_srand(0) as they are now aliases
$keys = array_rand($arr, 2); // not random either!
srand(); // or mt_srand() as they are now aliases
$keys = array_rand($arr, 2); // random again
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2a01:cb04:580:1500:af59:b3c2:2e85:b329)
----
Manual Page -- https://php.net/manual/en/function.srand.php
Edit -- https://main.php.net/note/edit/129127
Del: integrated -- https://main.php.net/note/delete/129127/integrated
Del: useless -- https://main.php.net/note/delete/129127/useless
Del: bad code -- https://main.php.net/note/delete/129127/bad+code
Del: spam -- https://main.php.net/note/delete/129127/spam
Del: non-english -- https://main.php.net/note/delete/129127/non-english
Del: in docs -- https://main.php.net/note/delete/129127/in+docs
Del: other reasons-- https://main.php.net/note/delete/129127
Reject -- https://main.php.net/note/reject/129127
Search -- https://main.php.net/manage/user-notes.php