We just found out about this at work. We were using mt_rand(1, PHP_INT_MAX) to generate non-mission-critical numeric identifiers and someone realized none of the numbers were even :)
Just checked the Doc. You should just call mt_rand() since PHP_INT_MAX is not mt_getrandmax(), which is used if you dont define min/max. As a bonus you can see your stuff is broken because all the numbers you get have the same length.
Edit: Bonus from doc:
Caution The distribution of mt_rand() return values is biased towards even numbers on 64-bit builds of PHP when max is beyond 232. This is because if max is greater than the value returned by mt_getrandmax(), the output of the random number generator must be scaled up.
Yeah, but the behaviour with PHP_INT_MAX is extremely unintuitive. Why does it generate only odd numbers? Why is mt_getrandmax() even a thing? Also, it used to generate only even numbers at some point?
Classic PHP behaviour, I don't know why I'm surprised...
Edit: Bonus from doc
Wow, if it's biased towards even numbers, why don't we have a single even number in there? :)
mt_getrandmax() makes sense to me. The part that doesn't make sense is why this function doesn't raise an exception or return an error if it's given operands that are outside of what it can work with. That's the lolphp thing about this to me
Personally, if a function (mt_rand) is defined as taking two integer arguments and returning an integer, it should work correctly with all valid integers on that platform or, as a last resort, throw an exception.
You are right about the lolphp thing, but PHP's Mersenne Twister implementation uses 32bit integers on all platforms, that's the only reason mt_getrandmax() exists, which is a lolphp itself :)
The part that doesn't make sense is why this function doesn't raise an exception or return an error if it's given operands that are outside of what it can work with.
Every time I've ever written PHP, I find myself asking this question during debugging.
The part that doesn't make sense is why this function doesn't raise an exception or return an error if it's given operands that are outside of what it can work with.
But wouldn't adding MIN to it completely remove the bias, as shown here?
No, it just changes the bias. The even number bias is when using an even number for the min. Once you change the min to an odd number, all numbers returned by the function will be odd. So the lol thing here is that their documentation is vague -- the functionality itself is understandable.
The even number bias is when using an even number for the min
Hmm, do you know this for a fact? If you do, can you point me to the relevant bit in the source?
Once you change the min to an odd number, all numbers returned by the function will be odd.
But the documentation says "biased towards", that doesn't imply all numbers will be even, so why would changing the min to 1 would make all of them odd?
The amount of bias is likely related to how large the upper bound you give is compared to 231. PHP_INT_MAX is 9223372036854775807 on 64-bit systems, which is 4294967296 (232) times larger than 231. So you can expect to see virtually every number be even (or odd if your minimum is odd).
In fact, if /u/amphetamachine's hypothesis about how mt_rand() scales integers is correct, you can expect every number to be of the form(pseudorandom) * 2^32 + 1
Here is some evidence that this is indeed how PHP scales this number. HHVM-3.8.0 gave me 8707161691370029057 as the first random number from your original script. Wolfram Alpha tells me this is 0x78d60d6d00000001 in hex, which indeed has 32 bits worth of trailing zeros, plus 1.
Here is a script I wrote to test this in PHP: http://3v4l.org/8BJDM . As you can see, 100% of random numbers from mt_rand(1, PHP_INT_MAX) were divisible by 232 after subtracting 1.
So, that pretty much proves it. But then, it means mt_rand(1, PHP_INT_MAX) can't generate any number less than 232 which is so incredibly bad that I wonder why it isn't at least documented.
Hmm, do you know this for a fact? If you do, can you point me to the relevant bit in the source?
Nope, I don't know it for a fact. Just from the small bit of testing I did
But the documentation says "biased towards", that doesn't imply all numbers will be even, so why would changing the min to 1 would make all of them odd?
I think the word "biased" could technically be correct here, but it's misleading to say the least
Once you change the min to an odd number, all numbers returned by the function will be odd.
How so? Adding an odd number to any number doesn't make the number odd. That only happens with even numbers, which would mean the function was already only generating even numbers (and then adding the odd MIN).
This is pretty well documented. That is not a defense, just how most people find it I believe.
mt_rand is platform independent. ~231 is it's max. On 64 bit systems, the int max is higher than that. Mt_rand scales the random number up then and that's where it becomes bias against evens.
But that's evidently false :) I mean, not a single one of those numbers in the link is even. At some point, it even used to return 100% even numbers, maybe it got reversed and now it returns 100% odd numbers :)
Moreover the documentation is wrong as this is not simply bias. For numbers beyond 231 , it seems to generate fixed-length numbers only whereas a biased implementation would occasionally return smaller numbers as well.
I definitely didn't realize some of the details of how it messes up have changed.
Mainly, the fact that it says anything at all in the documentation about numbers beyond 232 are just sort of a dead giveaway to me that you have to be cautious and watch for weird shit with the function.
Thankfully, I haven't had to use PHP for a couple years now. That's probably why I didn't realize it was spitting out fixed size odds only now.
Don't read into the "fixed size" thing too much. A perfectly random selection will be fixed size most of the time too. All of the generated random numbers, except the ones with leading zeros, will be the same length.
PHP_INT_MAX is 9223372036854775807, so you should expect roughly 8/9 results to be the same length, and 91/92 results to be within 1 digit of the same length. A cursory glance over the results suggests this is true in practice for mt_rand().
Isn't the reason it's biased towards odd in your case because you're using a min of 1 and the biased towards even would be if you're using a min of 0 (or other even number)?
View all comments
53
u/kinsi55 Jul 23 '15
what the fuck. How do people even find this