$max > getmaxrand() for rand() call
| From: | Leszek Krupinski | Date: | Sun, 12 Apr 2015 19:36:04 +0000 |
| Subject: | $max > getmaxrand() for rand() call | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-85776@lists.php.net to get a copy of this message | ||
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:
1. Document the behaviour and keep it that way.
2. Allow that behaviour, document it, but if $max > getrandmax(), emit a warning/notice.
3. Disallow that, document, and return false (as some other functions do when arguments are
incorrect).
4. 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