5 ms·
Why even accept ranges that span more than 2^32? That seems like an easy solution to a broken function. Also fun: echo "<?php echo mt_rand(-mt_getrandmax
by 3JPLW 11y ago
Why even accept ranges that span more than 2^32? That seems like an easy solution to a broken function.
Also fun:
echo "<?php echo mt_rand(-mt_getrandmax(), mt_getrandmax());" | php
is always even on my PHP 5.5.20.
- poizan42 11y agoWhy not generate two values from the underlying function and shift them into place if the range is larger than 2^32 -1? Oh well, ofc. because this is PHP which goes by the principle of most surprise.
- TillE 11y agoYes, that's exactly what sensible RNG interfaces do, like std::random in C++. If your underlying engine only produces 32 bits at a time, it'll grab two of them when you request a 64-bit type.
- TazeTSchnitzel 11y ago> Why even accept ranges that span more than 2^32? That seems like an easy solution to a broken function. It may never have been designed to. If it was written before 64-bit machines were commonplace, mt_getrandmax() would always be the same as PHP_INT_MAX.