Re: $max > getmaxrand() for rand() call
| From: | Rowan Collins | Date: | Mon, 13 Apr 2015 09:59:10 +0000 |
| Subject: | Re: $max > getmaxrand() for rand() call | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85778@lists.php.net to get a copy of this message | ||
Stanislav Malyshev wrote on 13/04/2015 04:23:
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:<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 :)
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]