Stanislav Malyshev wrote on 13/04/2015 04:23:
<https://bugs.php.net/bug.php?id=69396>) - to sum up: if you provide
$max parameter that is larger than OS-provided getmaxrand(), the
result will be artificially stretched, which in result leads to some
values not appearing at all. That’s troublesome if for instance some
people use that function to choose random server for a service, and
the stretched result wouldn’t return even values at all.
How many servers you've got? Also, why not use mt_rand()? It has max of
2^^31, if you have more servers than that you probably have very
uncommon use case :)

To be fair, that's not actually mutually exclusive with Leszek's suggestions - we still need to either document, or warn, that rand() has this limitation. The manual does actually mention using mt_rand(), but also suggests setting $max higher than getrandmax(), which is probably bad advice:

> If you require a range larger than 32767, specifying |min| and |max| will allow you to create a range larger than this, or consider using mt_rand() <http://php.net/manual/en/function.mt-rand.php> instead.

Regards,
--
Rowan Collins
[IMSoP]

Reply via email to