Hello
A friend of mine reported something that seemed like a bug (https://bugs.php.net/bug.php?id=69396 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.
The very same thing is reported as a first comment for the rand()
function in PHP manual - and that comment is 8 years old.
Apparently this is a sort-of correct behaviour, but it should be taken care of. I see 4 ways:
- Document the behaviour and keep it that way.
- Allow that behaviour, document it, but if $max >
getrandmax()
, emit a warning/notice. - Disallow that, document, and return false (as some other functions do when arguments are incorrect).
- Disallow that, but trim $max to
getrandmax()
, and emit a warning/notice.
I think that this behaviour should not be allowed, because in the end it generates incorrect results, doesn’t matter if the user would like that or not.
What do you think about this issue?
—Leszek
Hi!
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 :)
--
Stas Malyshev
smalyshev@gmail.com
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 usemt_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]
Rowan Collins wrote:
To be fair, that's not actually mutually exclusive with Leszek's
suggestions - we still need to either document, or warn, thatrand()
has
this limitation. The manual does actually mention usingmt_rand()
, but
also suggests setting $max higher thangetrandmax()
, 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.
It seems to be noteworthy that the mt_rand man page contains a
respective warning[1], and the mt_getrandmax man page explains the issue
in the "Return Values" section[2]. I think it would be reasonable to
document rand/getrandmax alike in this regard.
[1]
http://php.net/manual/en/function.mt-rand.php#refsect1-function.mt-rand-notes
[2]
http://php.net/manual/en/function.mt-getrandmax.php#refsect1-function.mt-getrandmax-returnvalues
--
Christoph M. Becker